Java 如果用户输入的数字超出参数范围,如何循环提示用户重新输入数字

Java 如果用户输入的数字超出参数范围,如何循环提示用户重新输入数字,java,while-loop,Java,While Loop,我正试图建立一个程序,用户(学生)输入他们每学期还剩多少门课程和打算修多少门课,程序将把这些数据形成一个数组,并打印出他们还剩多少学期。用户每学期不允许参加超过5门课程,因此我想提醒用户,他们输入的数字不正确,同时也为特定学生循环输入,而无需关闭控制台并重新运行程序。我试着在那里放置一个while(true){}循环来循环它,但是我似乎无法得到我想要的循环 我已经尝试将while(true){}循环放在代码的多个点中,但无法得到期望的结果 public static void main(Stri

我正试图建立一个程序,用户(学生)输入他们每学期还剩多少门课程和打算修多少门课,程序将把这些数据形成一个数组,并打印出他们还剩多少学期。用户每学期不允许参加超过5门课程,因此我想提醒用户,他们输入的数字不正确,同时也为特定学生循环输入,而无需关闭控制台并重新运行程序。我试着在那里放置一个while(true){}循环来循环它,但是我似乎无法得到我想要的循环

我已经尝试将while(true){}循环放在代码的多个点中,但无法得到期望的结果

public static void main(String[] args) {

  Scanner input = new Scanner(System.in);

  int[][] students = new int[10][2];

  for (int i = 0; i < students.length; i++) {
    System.out.println("Enter classes remaining and taking each term for student " + (i + 1) + ": ");
    for (int j = 0; j < students[i].length; j++) {
      students[i][j] = input.nextInt();

      if (students[i][1] > 5)
        System.out.println("The number of classes per term for student " + (i + 1) + " is invalid.");

    }
  }

  System.out.println();

  for (int i = 0; i < students.length; i++) {
    System.out.println("Student " + (i + 1) + " has " + (int) Math.round(students[i][0] / students[i][1]) + " terms left to graduate.");
  }

}
publicstaticvoidmain(字符串[]args){
扫描仪输入=新扫描仪(System.in);
int[][]学生=新int[10][2];
for(int i=0;i5)
System.out.println(“学生“+(i+1)+”的每学期课程数无效。”);
}
}
System.out.println();
for(int i=0;i

我希望第一次输入的输出打印“学生n每学期的班级数量无效”。并重复提示输入同一个学生n的班级数量,而不继续下一次学生输入。

这是根据您的新评论更新的数字。您应该从这里开始,根据需要进行更改

    public class StudentInfo
    {
   public static int totalStudents = 6;

   public static void main(String[] args)
   {

      Scanner input = new Scanner(System.in);

      int[][] Students = new int[totalStudents][3];
      // Student[i][0] will hold the Remaining classes.
      // Student[i][1] will hold the Classes per semester and
      // Student[i][2] will hold the number of total Semesters to complete all courses.

      for (int i = 0; i < totalStudents; i++)
      {
         System.out.print("\n\nEnter the information for " + (i + 1) + "-th student.");

         System.out.print("\n\nEnter the total number of remaining courses: ");
         Students[i][0] = input.nextInt();

         System.out.print("\nEnter the total number of courses per semester: ");
         Students[i][1] = input.nextInt();

         while (Students[i][1] > 5)
         {
            System.out.println("\nStudents are not allowed to take more than 5 classes per semester.");
            System.out.print("Enter the total number of courses per semester: ");
            Students[i][1] = input.nextInt();
         }

         int ts = Students[i][0] / Students[i][1];
         if (Students[i][0] % Students[i][1] != 0)
            ts++;

         Students[i][2] = ts;

         System.out.println("\nThis student needs a total of " + ts + " semesters to finish all courses.");
      }
      input.close();
   }
}
公共班级学生信息
{
公共静态学生总数=6;
公共静态void main(字符串[]args)
{
扫描仪输入=新扫描仪(System.in);
int[][]学生=新int[totalStudents][3];
//学生[i][0]将主持剩余的课程。
//学生[i][1]将每学期举办一次课程
//学生[i][2]将掌握完成所有课程的总学期数。
对于(int i=0;i5)
{
System.out.println(“\n学生每学期不得选修超过5门课。”);
System.out.print(“输入每学期的课程总数:”);
学生[i][1]=input.nextInt();
}
int ts=学生[i][0]/学生[i][1];
如果(学生[i][0]%学生[i][1]!=0)
ts++;
学生[i][2]=ts;
System.out.println(“\n该学生总共需要“+ts+”学期才能完成所有课程。”);
}
input.close();
}
}
公共静态void main(字符串[]args){
//扫描仪库
扫描仪输入=新扫描仪(System.in);
//初始化数组
int[][]学生=新int[10][2];
//迭代扫描和条件循环

对于(int i=0;其中print是println,下一个实际上是nextInt()但是注释框不会将其识别为代码。另外,我似乎对将数组中的每个输入四舍五入到下一个最高整数的结果打印输出有问题。JavaScript不是Java,我删除了标记并修复了一些代码格式问题。数组是赋值所需的。这是我在其中输入数据的唯一原因为什么你需要数组,数组中会存储什么?使用数组不是问题,请明确要求。这是一个简单的问题。要求将每个学生的值存储到一个数组中,然后通过数组完成数学计算。我面临的主要问题是让用户进入循环如果用户输入的第二个数字大于5,请再次输入学生n的数据。再次感谢。这意味着您的程序应该询问有多少学生,然后它会询问每个学生的剩余课程和每个学期的课程。然后它会找到每个学生所需的学期?对吗?我解决了这个问题,并将其发布在此处以供参考您投资了一个。这是一个嵌套问题,我在int J迭代循环中设置了这个周转条件。再次感谢您,如果您认为我的答案对您的问题是正确的,那么您可以在这里接受它作为正确的答案。@Jamell Passmore
public static void main(String[] args) {

    //scanner library
    Scanner input = new Scanner (System.in);

        //Initialize array
          int[][] students = new int [10][2];
            //iterate scanner and condition loop
              for(int i=0; i<students.length; i++){
                      System.out.print("Enter classes remaining and taking each term for student "+ (i+1) +": ");
                  for (int j=0; j<students[i].length;j++){
                          students[i][j]= input.nextInt();                            
                   }
                  while(students [i][1] > 5) {                                    
                      System.out.println("The number of classes per term for student " + (i+1) + " is invalid.");
                      i--;
                          break;            
                  }
              }

              System.out.println();
              //Print out results compiled from array
              for(int i =0; i<students.length; i++) {
                      System.out.println("Student "+(i+1)+" has "+ (int) Math.ceil((double)students[i][0]/students[i][1]) + " terms left to graduate.");  
              }
}