如何让用户在2D数组中输入5个标记?

时间:2018-04-22 15:26:24

标签: java

我需要创建一个允许用户输入标记的程序      评估。      我的代码在这里,但我的输出显示不正确。我不知道在哪里      我出错了。      这就是我到目前为止所做的工作

 public class Vetting{
 Scanner input = new Scanner(System.in);

   String TeacherName;
   int RatesArray[][] = new int [5][5];

int Assessment1,Assessment2,Assessment3,Assessment4,Assessment5;

      public int [][] RateArray()
      {   
       for(int row =0; row<RatesArray.length;row++)
      {

       for(int col = 0; col<RatesArray.length;col++)
       {

           RatesArray[row][col] = input.nextInt();
       }
   }

   return RatesArray;
   }

 public void OutputArray()
   {
       RateArray();
      System.out.println("The rates are: ");
       System.out.println("\t\t Assessment1, Assessment2, Assessment3, 
       Assessment4, Assessment5");

           System.out.println(" ");

           System.out.println("Student 1: \t");
           System.out.println("Student 2: \t");
           System.out.println("Student 3: \t");
           System.out.println("Student 4: \t");
           System.out.println("Student 5: \t");
       for(int row = 0; row<RatesArray.length;row++)
       {
            for(int col = 0; col<RatesArray.length;col++)
           {

               System.out.print(RatesArray[row][col] +"\t");
           }

           System.out.println("\n");

       }

   }

这是我的输出 My output

这应该是 The correct way

我将非常感谢能得到的任何帮助 谢谢

2 个答案:

答案 0 :(得分:0)

试试这个版本它应该打印一个有效的表...

Scanner scanner = new Scanner(System.in);
int [][] array = new int [5][5];

for(int studentIndex = 0; studentIndex < array.length; ++studentIndex)
{
    for(int gradeIndex = 0; gradeIndex < array[0].length; ++gradeIndex)
    {
        array[studentIndex][gradeIndex] = scanner.nextInt();
    }
}

// Prints table with headers

System.out.format("%20s%-20s%-20s%-20s%-20s%-20s%n", "", "Assesment 1", "Assesment 2", "Assesment 3",
            "Assesment 4", "Assesment 5");

for(int studentIndex = 0; studentIndex < array.length; ++studentIndex)
{
    System.out.format("%-20s", "Student " + studentIndex);

    for(int gradeIndex = 0; gradeIndex < array[0].length; ++gradeIndex)
    {
        System.out.format("%-20d", array[studentIndex][gradeIndex]);
    }

    System.out.format("%n");
}

顺便说一下,代码的主要问题是,它首先使用换行符打印所有行标题!你需要逐行打印......每一行都有它自己的行标题和它的值!此外,只有在完成一行后才需要打印换行符...

First row:  Student 0    5    7    5    1    2 -> Newline only here
Second row: Student 1    5    2    8    3    7 -> Newline only here
...

答案 1 :(得分:0)

删除以下行

 System.out.println("Student 1: \t");
 System.out.println("Student 2: \t");
 System.out.println("Student 3: \t");
 System.out.println("Student 4: \t");
 System.out.println("Student 5: \t"); 

问题:您在不同的行上打印学生,然后打印标记 只需将学生计数器移至for循环

即可
 for(int row = 0; row<RatesArray.length;row++)
       {
          System.out.print("Student "+(row+1)+": \t");//changes here
            for(int col = 0; col<RatesArray.length;col++)
           {

               System.out.print(RatesArray[row][col] +"\t");
           }

           System.out.println("\n");

       }