Hey I'm trying to calculate the of a quiz by comparing two arrays. The two arrays set up are the correct answers and student answers of a quiz. I'm trying to get it to look like this:
嘿,我试图通过比较两个数组来计算测验。设置的两个数组是测验的正确答案和学生答案。我试图让它看起来像这样:
Student Marks Average
学生成绩平均
Arnie Score: 4 Percentage: 0.8
Arnie得分:4%:0.8
This is because Arnie has got 4 questions correct out of 5 questions. The answers of the student and the correct answers are found in the text document which I already loaded onto an array on my code. The text document contains a 2D array with Arnie's answers in the form of chars and a 1D array containing the correct answers (also chars). There are more students but I would like some guidance on how to get the average for Arnie so I can do it for the rest of the names on my own.
这是因为Arnie在5个问题中有4个问题是正确的。学生的答案和正确答案可以在我已经加载到我的代码上的数组中的文本文档中找到。文本文档包含一个2D数组,其中包含字符形式的Arnie答案和包含正确答案(也是字符)的一维数组。有更多的学生,但我想要一些指导如何获得Arnie的平均值,所以我可以自己做其余的名字。
Here is my code so far with the two arrays set up.
到目前为止,这是我的代码,设置了两个数组。
public class workingCode
{
private static String newline = System.getProperty("line.separator");
public static void stuAnsOut(String arg []) throws IOException
{
File studentAns = new File("Ans_Stu.txt");
Scanner stuAns = new Scanner(studentAns);
String stuAnsString[] = new String[6];
char stuAnsArray[][] = new char[3][5];
String[] stuNameArray = new String[3];
for (int i = 0; i<stuAnsString.length;i++)
{
stuAnsString[i]=stuAns.nextLine();
}
for (int i=0,j=0; i<stuAnsArray.length && j<stuAnsString.length;i++, j=j+2)
{
stuNameArray[i] = stuAnsString[j];
}
for (int i=0,j=1; i<stuAnsArray.length && j<stuAnsString.length;i++, j=j+2)
{
stuAnsArray[i] = stuAnsString[j].toUpperCase().toCharArray();
}
System.out.println("Student Answers: ");
System.out.printf("%5s","Name");
for (int i =0; i<1;i++)
{
for (int j =1; j<(stuAnsArray[i].length+1);j++)
{
System.out.printf("%5s",j);
}
}
System.out.printf("%n");
for (int i =0; i<stuAnsArray.length;i++)
{
System.out.printf("%5s",stuNameArray[i]);
for (int j=0;j<stuAnsArray[i].length;j++)
{
System.out.printf("%5s",stuAnsArray[i][j]);
}
System.out.printf("%n");
}
}
public static void corAnsOut(String arg []) throws IOException
{
File correctAns = new File("Ans_Cor.txt");
Scanner corAns = new Scanner(correctAns);
String corAnsString = corAns.next();
char corAnsArray[] = new char[5];
for (int i=0; i<corAnsArray.length;i++)
{
corAnsArray[i] = corAnsString.toUpperCase().charAt(i);
}
System.out.println("Correct Answers: ");
System.out.println(newline);
for (int i =1; i<(corAnsArray.length+1);i++)
{
System.out.printf("%5s",i);
}
System.out.printf("%n");
for (int i =0; i<corAnsArray.length;i++)
{
System.out.printf("%5s",corAnsArray[i]);
}
System.out.printf("%n");
}
}
I am not allowed to use ArrayLists, only Arrays. And please use the array names I already have in my code. Thanks!
我不允许使用ArrayLists,只允许使用Arrays。请使用我的代码中已有的数组名称。谢谢!
Edit: This is what I got so far. But it is giving me an error:
编辑:这是我到目前为止所得到的。但它给了我一个错误:
public static void compareInteger(int corAnsArray [], int stuAnsArray[])
{double score =0;
for (int i = 0; i < stuAnsArray.length; i++)
{if(stuAnsArray[i] == corAnsArray[i])
score += 1.0;
}
System.out.println(score/corAnsArray.length);
}
2 个解决方案
#1
1
What i would do is create a class called Student
我要做的是创建一个名为Student的类
public class Student {
private string studentName;
private float marks;
private float average;
// Getters & all parameter constructor
}
Then change your working class to
然后将你的工人阶级改为
public class workingCode
{
private static String newline = System.getProperty("line.separator");
public static void stuAnsOut(String arg []) throws IOException
{
File studentAns = new File("Ans_Stu.txt");
Scanner stuAns = new Scanner(studentAns);
List<Student> students = new ArrayList<Student>();
while(stuAns.hasNext()) {
string parts = stuAns.nextLine().split(" ");
students.add(new Student(parts[0],parts[1],parts[2]))
}
// At this point you have all the students in the List
}
}
Now to calculate avg :
现在计算平均值:
float total = 0;
for(Student s : students) {
total += s.getMarks();
}
System.out.println("avg = " + total/students.size());
or
float total = 0;
for(Student s : students) {
total += s.getAverage();
}
System.out.println("avg = " + total);
#2
0
Code for comparing arrays (both for INT and String arrays):
比较数组的代码(INT和String数组):
public class Compare{
public static void main(String[] args){
double averageInt;
double averageString;
int[] correctInt = {9,8,12,6,3,12,90}; //Correct answers
int[] answerInt = {9,8,4,6,3,12,90}; //Student's answers
String[] correctString = {"A","A","B","C"}; //Correct answers
String[] answerString = {"A","C","B","C"}; //Student's answers
Compare compare = new Compare();
averageInt = compare.compareInteger(correctInt, answerInt);
averageString = compare.compareString(correctString, answerString);
System.out.println(averageInt);
System.out.println(averageString);
}
public double compareInteger(int[] correct, int[] answer){ //Compares the int arrays
double score = 0;
for(int i = 0; i < correct.length; i++){
if(correct[i] == answer[i])
score += 1.0;
}
return score/correct.length; //Returns average
}
public double compareString(String[] correct, String[] answer){ //Compares the String arrays
double score = 0;
for(int i = 0; i < correct.length; i++){
if(correct[i].equals(answer[i]))
score += 1.0;
}
return score/correct.length; //Returns average
}
}
I do not understand the marking system though. If you'd explain it more in-depth, I'd be more than happy to provide you with the code.
我不明白标记系统。如果您更深入地解释它,我将非常乐意为您提供代码。
#1
1
What i would do is create a class called Student
我要做的是创建一个名为Student的类
public class Student {
private string studentName;
private float marks;
private float average;
// Getters & all parameter constructor
}
Then change your working class to
然后将你的工人阶级改为
public class workingCode
{
private static String newline = System.getProperty("line.separator");
public static void stuAnsOut(String arg []) throws IOException
{
File studentAns = new File("Ans_Stu.txt");
Scanner stuAns = new Scanner(studentAns);
List<Student> students = new ArrayList<Student>();
while(stuAns.hasNext()) {
string parts = stuAns.nextLine().split(" ");
students.add(new Student(parts[0],parts[1],parts[2]))
}
// At this point you have all the students in the List
}
}
Now to calculate avg :
现在计算平均值:
float total = 0;
for(Student s : students) {
total += s.getMarks();
}
System.out.println("avg = " + total/students.size());
or
float total = 0;
for(Student s : students) {
total += s.getAverage();
}
System.out.println("avg = " + total);
#2
0
Code for comparing arrays (both for INT and String arrays):
比较数组的代码(INT和String数组):
public class Compare{
public static void main(String[] args){
double averageInt;
double averageString;
int[] correctInt = {9,8,12,6,3,12,90}; //Correct answers
int[] answerInt = {9,8,4,6,3,12,90}; //Student's answers
String[] correctString = {"A","A","B","C"}; //Correct answers
String[] answerString = {"A","C","B","C"}; //Student's answers
Compare compare = new Compare();
averageInt = compare.compareInteger(correctInt, answerInt);
averageString = compare.compareString(correctString, answerString);
System.out.println(averageInt);
System.out.println(averageString);
}
public double compareInteger(int[] correct, int[] answer){ //Compares the int arrays
double score = 0;
for(int i = 0; i < correct.length; i++){
if(correct[i] == answer[i])
score += 1.0;
}
return score/correct.length; //Returns average
}
public double compareString(String[] correct, String[] answer){ //Compares the String arrays
double score = 0;
for(int i = 0; i < correct.length; i++){
if(correct[i].equals(answer[i]))
score += 1.0;
}
return score/correct.length; //Returns average
}
}
I do not understand the marking system though. If you'd explain it more in-depth, I'd be more than happy to provide you with the code.
我不明白标记系统。如果您更深入地解释它,我将非常乐意为您提供代码。