简体   繁体   English

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

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

I'm trying to set up a program where the user(student) inputs how many courses they have left to graduate and and how many classes the intend to take per terms and the program will form this data into an array and print out how many terms they have left.我正在尝试建立一个程序,用户(学生)输入他们还有多少课程要毕业,以及每个学期打算上多少门课,程序会将这些数据形成一个数组并打印出多少他们留下的条款。 The user is not allowed to take more than 5 courses per term so I want to prompt the user that the number they input is incorrect while also looping that input for that specific student without have to close the console and re-run the program.用户每学期不得参加超过 5 门课程,因此我想提示用户他们输入的数字不正确,同时还为该特定学生循环输入,而无需关闭控制台并重新运行程序。 I've tried placing a while(true){} loop there in order to loop it but i can't seem to get the loop i desire.我尝试在那里放置一个 while(true){} 循环以便循环它,但我似乎无法获得我想要的循环。

I've tried placing the while(true){} loop in multiple spots of the code and can't get the desired result.我尝试将 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.");
  }

}

I expect the output for the first input to print","The number of class per term for student n is invalid." and repeat the prompt to enter the numbers for that same student n without proceeding to the next student input.我希望 output 用于打印的第一个输入“”“学生 n 每学期 class 的数量无效。”并重复提示以输入同一学生 n 的数字,而无需继续下一个学生输入。

Here is the updated one based on your new comments.这是基于您的新评论的更新。 You should be good from here, make changes whatever you need.你应该从这里开始,做任何你需要的改变。

    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.

相关问题 提示用户重新输入整数直到输入二进制数 - Prompting user to re-enter integers until binary number is entered 在第二个for循环中,如何使用户重新输入考试分数(如果为负)? - How to do I make the user re-enter the exam scores if its negative, in the second for loop? 当要求用户在Java中循环输入一些字符串时,如何仅保留文本打印一次 - How to keep Text print only once when asking a user to re-enter some string in loop in Java 如果发生错误,如何提示用户重新输入值,以及如何提供继续检查工资率或退出程序的选项? - How to prompt the user to re-enter a value if a mistake was made and How to give an option to keep on checking pay rates or to exit the program.? Java-异常处理-如何重新输入无效输入 - Java - Exception handling - How to re-enter invalid input 我如何要求用户重新输入他们的选择? - How can I ask the user to re-enter their choice? 我有办法重新输入错误的输入吗? - Is there a way for me to re-enter a wrong input? 试图提示用户重新进入catch块,但是catch块终止了吗? - Trying to prompt the user to re-enter from the catch block, but the catch block terminates? 如果用户输入具有多个空格的字符串,如何重新提示用户输入 - How to re-prompt a user for input if they enter a string with multiple whitespaces 如何提示用户输入有效的手机号码? - How to prompt user to enter a valid mobile phone number?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM