Java 数组:将一个数组与另一个数组进行比较以找到平均值
嘿,我想通过比较两个数组来计算测验的结果。设置的两个数组是测验的正确答案和学生答案。我想让它看起来像这样: 学生平均成绩 阿尼得分:4个百分点:0.8 这是因为阿尼在5个问题中有4个问题是正确的。学生的答案和正确答案可以在文本文档中找到,我已经将其加载到代码的数组中。文本文档包含一个2D数组,其中Arnie的答案以字符的形式显示,而1D数组包含正确的答案(也是字符)。有更多的学生,但我想得到一些关于如何获得阿尼平均分的指导,这样我就可以自己为其余的名字做 这是我到目前为止设置了两个数组的代码Java 数组:将一个数组与另一个数组进行比较以找到平均值,java,arrays,Java,Arrays,嘿,我想通过比较两个数组来计算测验的结果。设置的两个数组是测验的正确答案和学生答案。我想让它看起来像这样: 学生平均成绩 阿尼得分:4个百分点:0.8 这是因为阿尼在5个问题中有4个问题是正确的。学生的答案和正确答案可以在文本文档中找到,我已经将其加载到代码的数组中。文本文档包含一个2D数组,其中Arnie的答案以字符的形式显示,而1D数组包含正确的答案(也是字符)。有更多的学生,但我想得到一些关于如何获得阿尼平均分的指导,这样我就可以自己为其余的名字做 这是我到目前为止设置了两个数组的代码 p
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");
}
}
公共类工作代码
{
私有静态字符串newline=System.getProperty(“line.separator”);
公共静态void stuAnsOut(字符串arg[])引发IOException
{
File studentAns=新文件(“Ans_Stu.txt”);
扫描器stuAns=新扫描器(学生);
字符串stuAnsString[]=新字符串[6];
char stuAnsArray[][]=新字符[3][5];
字符串[]数组=新字符串[3];
对于(inti=0;i我要做的是创建一个名为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
}
}
或
用于比较数组(INT数组和字符串数组)的代码:
公共类比较{
公共静态void main(字符串[]args){
双平均点;
双平均字符串;
int[]correctInt={9,8,12,6,3,12,90};//正确答案
int[]answerInt={9,8,4,6,3,12,90};//学生答案
String[]correctString={“A”、“A”、“B”、“C”};//正确答案
String[]answerString={“A”、“C”、“B”、“C”};//学生答案
比较=新的比较();
averageInt=compare.compareInteger(correctInt,answerInt);
averageString=compare.compareString(correctString、answerString);
System.out.println(averageInt);
System.out.println(平均字符串);
}
公共双比较整数(int[]正确,int[]答案){//比较int数组
双倍得分=0;
对于(int i=0;i
不过我不理解评分系统。如果你能更深入地解释一下,我很乐意为你提供代码。你坚持哪一部分?因此第一部分是每个学生的平均分。所以我应该输出阿尼的分数和他们得到的百分比。要做到这一点,我知道我必须比较正确的answer数组到学生答案数组。这是怎么做到的?假设我使用另一种方法,我如何调用两个数组并找到Arnie的分数并计算平均值?谢谢。好的,但我不允许使用ArrayList,只能使用数组。@Nb179将列表
更改为学生[]
所以第一个数字是分数,第二个是百分比。所以阿尼得4分,百分比是0.8(4/5)。
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);
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
}
}