繁体   English   中英

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

[英]How to loop a prompt for a user to re-enter a number if they input a number outside of the parameters

我正在尝试建立一个程序,用户(学生)输入他们还有多少课程要毕业,以及每个学期打算上多少门课,程序会将这些数据形成一个数组并打印出多少他们留下的条款。 用户每学期不得参加超过 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.");
  }

}

我希望 output 用于打印的第一个输入“”“学生 n 每学期 class 的数量无效。”并重复提示以输入同一学生 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();
   }
}
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.");  
              }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM