Java-如何比较一维数组和二维数组

标签 java arrays

我无法比较一维数组和二维数组。我已经将 txt 文件导入一维数组和二维数组。一维数组包含 20 个正确答案(True 和 False)。二维数组包含 2000 个学生答案(100 个学生 * 每个学生 20 个答案)。

我想设计一个程序来显示每个学生的分数,我已经尝试过编程。你能帮我找出我哪里做错了吗?

程序的第二部分是打印每个问题,有多少学生答对了每个问题?

非常感谢!!

import java.io.*;
import java.util.Arrays;

public class Scores
{
  public static void main(String[] args) {

  try {
    File AnswerFile = new File("/Users/shaovera/NetBeansProjects/Scores/QuestionAnswer.txt");
    FileReader AnswerReader = new FileReader(AnswerFile);
    BufferedReader answerreader = new BufferedReader(AnswerReader);

    String[] words = new String[20];
    for(int a = 0; a < words.length; a++) {
      words[a] = answerreader.readLine();
      System.out.println(words[a]);
    }
    answerreader.close();
  }
  catch (Exception ex) {
    ex.printStackTrace();
  }

  try {
    File StudentFile = new File("/Users/shaovera/NetBeansProjects/Scores/StudentAnswer.txt");
    FileReader StudentReader = new FileReader(StudentFile);
    BufferedReader studentreader = new BufferedReader(StudentReader);

    String[][] table = new String[100][20];

    for (int i = 0; i < table.length; i++) {
      for(int j = 0; j < table[i].length; j++) {
        table[i][j] = studentreader.readLine();
        System.out.print(table[i][j] + " ");

      }
      System.out.println("");
    }
    studentreader.close();
  }
  catch (Exception ex) {
    ex.printStackTrace();
  }

  int count=0;
  int student=0;    
  String[] words = new String[20];
  String[][] table = new String[100][20];
  for (int column = 0; column < words.length; column++) {
    for (int row = 0; row < 100; row++) {
      if (words[row] == table[row][column]) {
        count++; 
        student++;
        System.out.print("student#" + student + ":" + count);
      }
    } 
  }
}

最佳答案

我认为这个代码是正确的(当你遇到问题时我在行中写了注释)

public static void main(String[] args){
    String[]words= new String[20]; // before load from file
    String[][]table =new String[100][20]; // before load from file

try{
    File AnswerFile=new File("/Users/shaovera/NetBeansProjects/Scores/QuestionAnswer.txt");
    FileReader AnswerReader = new FileReader(AnswerFile);
    BufferedReader answerreader = new BufferedReader(AnswerReader);


    for(int a=0; a<words.length;a++){
        words[a]=answerreader.readLine();
        System.out.println(words[a]);
    }
    answerreader.close();
}
catch (Exception ex){
    ex.printStackTrace();
}

try{
    File StudentFile=new File("/Users/shaovera/NetBeansProjects/Scores/StudentAnswer.txt");
    FileReader StudentReader = new FileReader(StudentFile);
    BufferedReader studentreader = new BufferedReader(StudentReader);           

    for (int i = 0; i <table.length; i++) {
        for(int j=0;j < table[i].length; j++){
            table[i][j]= studentreader.readLine();
            System.out.print(table[i][j]+" ");

        }
        System.out.println("");
    }
    studentreader.close();
}

catch (Exception ex){
    ex.printStackTrace();
}

    for (int column = 0; column < words.length; column++) {
        int student = 0;
        for (int row = 0; row < table.length; row++) {                            
            if (Objects.equals(words[column],table[row][column])) {
                student++;
            }
        }            
        System.out.println("student#" + student + ":" + column);
    }
}

关于Java-如何比较一维数组和二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34006135/

相关文章:

JavaScript 比较数组在不应该失败的时候失败

php - PDO 比较两个不同格式的数组

javascript - 使用 JavaScript 在单个函数内将数字数组转换为字符串,然后再转换回数字数组

java - 删除字符串

java.lang.String 无法转换为 java.io.InputStream

java - JTabbedPane:选项卡左侧的图标

const char **name VS char *name[]

arrays - 我可以使用 -v 将数组传递给 awk 吗?

java - 通过添加空格使字符串大小相等

java - 将数据类型字符串中的数字转换为 char