【问题标题】:ragged array not letting me have differing columns参差不齐的数组不允许我有不同的列
【发布时间】:2019-04-24 22:19:36
【问题描述】:

所以基本上我在这里的这个程序有一个问题,它应该让老师输入他们想要输入多少学生的分数,然后为每个学生输入多少测试分数 - 使用参差不齐的数组。不幸的是,我的数组不允许任何学生获得不同数量的分数。我真的不确定我做错了什么。

`导入java.util.Scanner;

    public class part2
    {
       public static void main (String[] args)
  {
  //Declarations 
  int num;
  int count = 0;
  int scores = 0;
  int sum = 0;
  double average = 0;
  int check = 1;
  int each = 0;
 //Open new scanner
    Scanner kbd = new Scanner (System.in);

 //Get number of students
    System.out.println("Please enter how many students you would like to enter scores for: ");
    num = kbd.nextInt();

 //Create ragged array
    int[][] ragged = new int[num][];


 //Get number of scores for each student
    for(int i = 0; i < num; i++)
    {
        count++;
        System.out.println("For student #" + count + " how many scores do you have? ");
        scores = kbd.nextInt();

        ragged[i] = new int[scores];

    }

    count = 0;
    //Get each student's score

    for(int i = 0; i < num; i++)
    {
        count++;
        for(int j = 0; j < scores; j++)
        {

        System.out.println("Student  #"+ count + "'s " + (j + 1) + "'st score is: " );
        each = kbd.nextInt();

        ragged[i][j] = each;
        }
    }

    //Get each student's average of scores
    for(int i = 0; i < num; i++)
    {
        check++;
        for(int j = 0; j < scores; j++)
        {

            sum += ragged[i][j];
            average = ((double)sum / (double)scores);


        }

        System.out.println("The Average for student #" + (check - 1) + " is: " + average);

    }



    //Housekeeping
    kbd.close();

} }`

【问题讨论】:

    标签: java


    【解决方案1】:

    首先,不要预先声明局部变量。您通常应该在(首先)分配它们的位置声明它们。

    如果您这样做,局部变量通常在代码块内声明,即局部变量的范围仅限于该代码块,并且在代码块结束时不再可用。这将对您有所帮助,因为当您使用不再相关的变量值时,编译器现在可以告诉您(错误消息)。

    喜欢你的 scores 变量。它是在循环内赋值的,循环结束后,它会有 last 值,即对其余代码完全无用的值。

    所以,在循环内移动声明:

     //Get number of scores for each student
        for(int i = 0; i < num; i++)
        {
            count++;
            System.out.println("For student #" + count + " how many scores do you have? ");
            int scores = kbd.nextInt(); // <=== Declare it here
    
            ragged[i] = new int[scores];
    
        }
    

    现在您在错误地尝试使用它时会出现编译错误。那是因为在这些地方,您需要获取与相关行相关的值。如果你愿意,你可以在那里重新声明和初始化变量:

    count = 0;
    //Get each student's score
    
    for(int i = 0; i < num; i++)
    {
        count++;
        int scores = ragged[i].length; // <=== Re-declare and initialize here
        for(int j = 0; j < scores; j++)
        {
    
        System.out.println("Student  #"+ count + "'s " + (j + 1) + "'st score is: " );
        each = kbd.nextInt();
    
        ragged[i][j] = each;
        }
    }
    

    你需要再做一次,你的代码应该是好的(无论如何,在粗糙的部分)。

    【讨论】:

      【解决方案2】:

      有一种非常简单的方法可以做到这一点。你只需要访问每个学生的分数,你可以这样做:

      for(int i = 0; i < num; i++)
      {
          count++;
          for(int j = 0; j < ragged[i].length; j++)
          {
      
              System.out.println("Student  #"+ count + "'s " + (j + 1) + "'st score is: " );
              each = kbd.nextInt();
      
              ragged[i][j] = each;
          }
      }
      

      ragged[i].length 行将分别访问每个数组中的元素数量,然后您可以为每个学生设置不同数量的分数

      【讨论】:

        猜你喜欢
        • 2013-08-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-25
        相关资源
        最近更新 更多