簡體   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