如果用户在参数外输入数字,如何循环提示用户重新输入数字

时间:2019-10-07 17:20:35

标签: java while-loop

我正在尝试建立一个程序,在该程序中,用户(学生)输入他们要毕业的课程数量以及每学期打算修多少课,该程序会将这些数据形成数组并打印出来剩下多少个名词用户每学期不得参加超过5门课程,因此我想提醒用户输入的数字不正确,同时还要为该特定学生循环输入,而不必关闭控制台并重新运行程序。我尝试在其中放置while(true){}循环以进行循环,但我似乎无法获得我想要的循环。

我尝试将while(true){}循环放置在代码的多个位置,但无法获得预期的结果。

HW_RTC_GetCalendarValue()

我希望打印的第一个输入的输出是“学生n每学期的班级数量无效。”,然后重复提示输入该学生n的编号,而无需继续下一个学生输入。

2 个答案:

答案 0 :(得分:0)

这是根据您的新评论更新的内容。从这里开始您应该很好,可以根据需要进行更改。

    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();
   }
}

答案 1 :(得分:0)

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.");  
              }
}