java - 如何正确显示数组的内容

标签 java arrays

我正在尝试制作一个测试修正模拟器,用户首先输入测试反馈,然后输入类(class)中的学生人数、姓名,最后输入答案。但问题是:当我尝试显示时学生姓名和答案...它们位于另一个下方。那么我该如何解决这个问题?

这个问题是因为我制作了两个for's而不是一个?所以我无法正确显示学生姓名和答案?

我的另一个问题是学生的分数。我如何为每个学生制作分数并将其显示在侧面。

检查下面的堆栈跟踪以更好地理解:

enter image description here

import java.util.Scanner;

public class testArray {
    public static void main(String[] args) {
        int score = 0, n, i, j = 0;
        Scanner sc = new Scanner(System.in).useDelimiter("\r\n");

        System.out.print("How many students in the class?: ");
        n = sc.nextInt();

        String name[] = new String[n];
        char answer[][] = new char[n][5];
        float media[] = new float[n];
        char testFeedback[] = new char[5];
        int socreArray[] = new int[n];

        System.out.println("--Type (a,b,c,d or e)--");
        for (int g = 0; g < testFeedback.length; g++) {
            System.out.print("TestFeedback | question[" + g + "]: ");
            testFeedback[g] = sc.next().charAt(0);
        }

        for (i = 0; i < n; i++) {
            System.out.print("Student name: ");
            name[i] = sc.next();

            for (j = 0; j < 5; j++) {
                System.out.println("answer nº" + (j + 1) + ": ");
                answer[i][j] = sc.next().charAt(0);

                if (answer[i][j] == testFeedback[j]) {

                    score++;
                }

            }

            System.out.println("Score: " + score);

        }

        System.out.println("\nGeneral report: ");
        System.out.println("Student - answer - score");

        for (i = 0; i < n; i++) {
            System.out.println(name[i]);

        }

        for (j = 0; j < n; j++) {
            System.out.println(answer[j]);

        }

        if (score > 3) {
            System.out.println("Congratulations, you went well!");

        } else {
            System.out.println("Too bad, you need to study more.");
        }

    }
}

最佳答案

如果我理解你的问题正确,要显示姓名和答案,你可以使用 1 来表示:

for (i = 0; i < n; i++) {
     System.out.print(name[i] + ": ");
     for(int j = 0; j < answer[i].length; j++) {
         System.out.print(answer[i][j] + " ");
     }
     System.out.println();

}

要显示分数,您必须将其存储在某个位置,例如另一个数组或列表中。

关于java - 如何正确显示数组的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35469531/

相关文章:

基于像素模式识别的自动化Java框架

java - 将句柄传递给 java 中的子类时,我应该使用声明还是 this() ?

java - FileUploadBase 上传小文件时找不到任何分段部分

java - 为什么JAVA中getWidth函数返回的值更多?

Java对象和数组内存位置

Python numpy 屏蔽数组初始化

c# - 如何实现二维矩阵的Kadane算法

python - Dataframe 到 numpy 数组中,值以逗号分隔

java - 执行捕获异常中的前面步骤

java - 无法正确计算Java中的二维数组