簡體   English   中英

字符串輸入的java用戶驗證,直到給出正確的輸入

[英]java user validation for string input until correct input is given

我將如何使用 while 循環不斷要求用戶重復輸入有效答案,直到給出有效輸入並在給出有效輸入時結束程序? 我只知道如何使用int和 numbers。 我對字母感到困惑。 我應該如何應用 NOT 運算符或其他邏輯運算符|| && || &&

Scanner myScan = new Scanner(System.in);                                        
System.out.println("Enter food");                                       
String food = myScan.nextLine();

if (food.equalsIgnoreCase("b"))
{
    System.out.println("beans");
}
else if (food.equalsIgnoreCase("e"))                                        
{
    System.out.println("eggs");
}
else
{
    System.out.println("error");
}

一種方法是定義一個“標志”變量,然后循環直到它為“真”。 例如:

  Scanner myScan = new Scanner(System.in);
  boolean done = false;
  while (!done) {                                        
    System.out.println("Enter food");                                       
    String food = myScan.nextLine().toLowerCase();                                        
    switch (food) {
       case "b" :
         System.out.println("beans");                                      
         done = true;
         break;
       case "e" :
         System.out.println("eggs");                                       
         done = true;
         break;
       ...
       default :
         System.out.println("error");                                      
     }    
  }    

在一個非常簡單的層面上,我會使用do-while循環,因為您想至少進入循環一次。 然后我會使用boolean標志來確定輸入的有效性,並基於此做出進一步的決定,例如......

Scanner myScan = new Scanner(System.in);
boolean userInputCorrect = false;
String food = null;
do {
    System.out.println("Enter food");
    food = myScan.nextLine();
    userInputCorrect = food.equalsIgnoreCase("b") || food.equalsIgnoreCase("e") || food.equalsIgnoreCase("exit");
    if (!userInputCorrect) {
        System.out.println("Error");
    }
} while (!userInputCorrect);
System.out.println("You selected " + food);

擴展的解決方案可能會使用某種valid方法,我可以將String傳遞給該方法並讓它根據已知值驗證輸入,但這可能有點超出了問題的范圍

正如已正確指出的那樣,但其他人指出,將輸入轉換為小寫一次並比較所有值會更有效,在這種情況下,最好使用switch語句......

Scanner myScan = new Scanner(System.in);
boolean userInputCorrect = false;
String food = null;
do {
    System.out.println("Enter food");
    food = myScan.nextLine();
    switch (food.toLowerCase()) {
        case "b":
        case "e":
        case "exit":
            userInputCorrect = true;
            break;
        default:
            System.out.println("Error");
    }
} while (!userInputCorrect);
System.out.println("You selected " + food);

但你也可以這樣做...

food = myScan.nextLine().toLowerCase();
userInputCorrect = food.equals("b") || food.equals("e") || food.equals("exit");

您也可以使用Map ,不用擔心添加更多食物

    Scanner myScan = new Scanner(System.in);
    System.out.println("Enter food");
    String food;

    Map<String, String> foodMap = new HashMap<>();
    foodMap.put("e", "Eggs");
    foodMap.put("b", "Beans");
    // add more food
    while (true) {
        food = myScan.nextLine();
        String value = foodMap.get(food);
        if (value == null) {
            System.out.println("Error!");
            break;
        }
        System.out.println(value);
    }

首先是使用標志的壞習慣,你應該使用的是一種叫做Do while Loop 的東西。

do { 
//block of code to be executed
} while(booleanExpression);

do while 循環將do塊中的代碼,然后檢查while表達式。


對於您的情況,您可以執行以下操作:

Scanner myScan = new Scanner(System.in);  
String food;
do{
  System.out.println("Enter food");                                       
  food = (myScan.nextLine()).toLoweCase();
}while(!food.equals("b") || !food.equals("c"))

if (food.equals("b"))                                        
{                                       
 System.out.println("beans");                                      
}                                       
else if (food.equals("e"))                                        
{                                       
 System.out.println("eggs");                                       
}        

如果食物的數量繼續增長,您可能會遇到問題,以備將來證明。 考慮使用數組或數組列表

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM