数组:将一个数组与另一个数组进行比较以查找平均数

时间:2017-01-25 20:42:46

标签: java arrays

嘿我试图通过比较两个数组来计算测验。设置的两个数组是测验的正确答案和学生答案。我试图让它看起来像这样:

学生成绩平均值

Arnie得分:4%:0.8

这是因为Arnie在5个问题中有4个问题是正确的。学生的答案和正确的答案可以在我已经加载到我的代码上的数组中的文本文档中找到。文本文档包含一个2D数组,其中包含字符形式的Arnie答案和包含正确答案(也是字符)的一维数组。有更多的学生,但我想要一些指导如何获得Arnie的平均值,所以我可以自己做其余的名字。

到目前为止,这是我的代码,设置了两个数组。

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"); 
    }
}

我不允许使用ArrayLists,只允许使用Arrays。请使用我的代码中已有的数组名称。谢谢!

编辑:这是我到目前为止所得到的。但它给了我一个错误:

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 个答案:

答案 0 :(得分:1)

我要做的是创建一个名为Student

的类
public class Student {
  private string studentName;
  private float marks; 
  private float average;

  // Getters & all parameter constructor 
}

然后将您的工人阶级改为

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
    }
}

现在计算平均值:

float total = 0;
for(Student s : students) {
   total += s.getMarks();
}
System.out.println("avg = " + total/students.size());

float total = 0;
for(Student s : students) {
   total += s.getAverage();
}
System.out.println("avg = " + total);

答案 1 :(得分:0)

比较数组的代码(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
    }
}

我不明白标记系统。如果您更深入地解释它,我将非常乐意为您提供代码。