当前位置: 首页 > 知识库问答 >
问题:

当循环错误正在重复我的用户输入提示时

闾丘坚诚
2023-03-14
// Import packages and create main function
import java.util.Scanner;
import java.util.ArrayList;
public class schedule{
   public static void main(String []args){
      // Creates scanner and ArrayLists
      Scanner scanner = new Scanner(System.in);
      ArrayList<Integer> mechanics = new ArrayList<Integer>();
      ArrayList<Integer> bays = new ArrayList<Integer>();
      boolean edit = true;
      while(edit == true){
         edit  = editSchedule(scanner);
         // Runs AddorRemove or ends program based on user's previous choice
         if(edit == true){
            if(AddorRemove(scanner) == true){
               add(scanner, mechanics, bays);
            }
            else{
               remove(scanner, mechanics, bays);
            }
         }
         else{
            System.out.println("Have a nice day!");
         }
      }
   }
   
   // Asks user if they want to edit the schedule
   public static boolean editSchedule(Scanner scanner){
      String answer = "b";
      while(!(answer.equals("y")) || answer.equals("n")){
         System.out.print("Would you like to edit the schedule? (y/n): ");
         answer = scanner.nextLine();
         if(answer.equals("y") || answer.equals("n")){
            break;
         }
         else{
            System.out.println("Invalid response.");
         }
         
      }
      if(answer.equals("y")){
         return true;
      }
      else{
         return false;
      }
   }
   
   // Asks user if they want to add or remove an element
   public static boolean AddorRemove(Scanner scanner){
      String aor = "b";
      while(!(aor.equals("a")) || aor.equals("r")){
         System.out.print("Do you want to add or remove a mechanic and bay? (a/r): ");
         aor = scanner.nextLine();
         if(aor.equals("a") || aor.equals("r")){
            break;
         }
         
      }
      if(aor.equals("a")){
         return true;
      }
      else{
         return false;
      }
   }
   
   // Checks to see that elements are not present already and adds element to the mechanic and bay ArrayLists
   public static void add(Scanner scanner, ArrayList<Integer> mechanics, ArrayList<Integer> bays){
      System.out.print("Enter the mechanic's ID number: ");
      int mechanic = scanner.nextInt();
      System.out.print("Enter the bay number: ");
      int bay = scanner.nextInt();
      boolean shouldAdd = true;
      for(int i=0; i<=mechanics.size()-1; i++){
         if(mechanic == mechanics.get(i)){
            System.out.println("Mechanic " + mechanic + " is already booked.");
            shouldAdd = false;
         }
      }
      for(int j=0; j<=bays.size()-1; j++){
         if(bay == bays.get(j)){
            System.out.println("Bay " + bay + " is already booked.");
            shouldAdd = false;
         }
      }
      if(shouldAdd == true){
         mechanics.add(mechanic);
         bays.add(bay);
         System.out.println("Mechanic " + mechanic + " booked for bay " + bay + ".");
      }
      else{
         System.out.println("Could not book mechanic " + mechanic + " in bay " + bay + ".");
      }
   }
   
   // Removes an element from the mechanic and bay ArrayLists
   public static void remove(Scanner scanner, ArrayList<Integer> mechanics, ArrayList<Integer> bays){
      System.out.println("remove");
   }
}
Would you like to edit the schedule? (y/n): y
Do you want to add or remove a mechanic and bay? (a/r): a
Enter the mechanic's ID number: 1
Enter the bay number: 3
Mechanic 1 booked for bay 3.
Would you like to edit the schedule? (y/n): Invalid response.
Would you like to edit the schedule? (y/n):

共有1个答案

杜浩壤
2023-03-14

当您使用scanner.nextint()时,它不会完成这一行。请将此测试作为一个示例:

@Test
public void testScanner() {
    Scanner scanner = new Scanner("1\n2\n3\n4\n");
    System.out.println("nextLine: [" + scanner.nextLine() + "]");
    System.out.println("nextInt:  [" + scanner.nextInt() + "]");
    System.out.println("nextLine: [" + scanner.nextLine() + "]");
    System.out.println("nextLine: [" + scanner.nextLine() + "]");
}

它产生:

nextLine: [1]
nextInt:  [2]
nextLine: []
nextLine: [3]

因此,在add()方法返回后,将有一个行结束,扫描仪将以空行的形式读取该行。这会给出无效响应消息。

 类似资料: