简体   繁体   中英

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. 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.

I've tried placing the while(true){} loop in multiple spots of the code and can't get the desired result.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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