簡體   English   中英

驗證用戶輸入的字符串正確

[英]Verify that String input by user is correct

我有一個字符串數組,例如:

String [] teams = {"Manchester City", "Chelsea", "Barcelona", "Bayern Munich", "Juventus", "Tottenham", "Liverpool", "Basel", "PSG", "Real Madrid", "Porto" + 
"Besiktas", "Sevilla", "Manchester United", "Roma", "Shaktar Donetsk"};

我需要幫助的程序代碼:

System.out.print("\nWho do you think will win?: ");
String winner = sc.nextLine();

team: while (teamValid) {

    for (int i = 0; i < teams.length ; i++) {

         if (!(winner.equalsIgnoreCase(teams[i])))
             counter++;

         if (counter == 16) {
             System.out.print("\nWho do you think will win?: ");
             winner = sc.nextLine();    
             teamValid = false;
             break;
         }

        if ((winner.equalsIgnoreCase(teams[i]))) {
             break team;
        }
    }

}

System.out.println(winner);

我要做的是提示用戶在此String數組中輸入團隊之一。 如果他輸入的名字有誤(忽略大小寫),那么我再次要求進入該團隊。 這就是我所擁有的。 我了解我的邏輯錯誤發生在:

if (counter == 16) {
    System.out.print("\nWho do you think will win?: ");
    winner = sc.nextLine(); 
    teamValid = false;
    break;
}

我怎樣才能解決這個問題?

您正在遍歷團隊,這沒有任何意義。

相反,您應該嘗試:

  1. 執行Do-While而不是While
  2. 僅當您閱讀答案(含義(偽代碼))時,才能遍歷列表:

     do { potentialWinner = sc.read() for (each team){ if (potentialWinner.ignoreCaseEquals(team) winner = potentialWinner break } } while (winner == null) 

您可以改用do while循環,並且在Java 8中,您可以這樣解決問題:

String winner; boolean check;
do {
    System.out.print("\nWho do you think will win?: ");
    winner = sc.nextLine();
    //check if there are a team in your array, with (ignoring cases)
    check = Arrays.asList(teams).stream().anyMatch(winner::equalsIgnoreCase);

} while (!check);

System.out.println(winner);

考慮這樣的事情:

input: while (true) {
    String winner = sc.nextLine();
    for (String name : teams) {
        if (winner.equalsIgnoreCase(name)) {
            break input;
        }
    }
}

這將接受一個輸入,並檢查它是否存在於數組中。 如果沒有,它將提示用戶進行其他輸入,並在數組中反復進行,直到找到有效的輸入為止。

假設您使用的是Java 8+,則可以使用Arrays.asListString[]轉換為List ,然后在do-while循環中使用winner::equalsIgnoreCase中的Predicate<String> 並且您可以對Predicate求反negate()來確定“其他人”。 喜歡,

String[] teams = { "Manchester City", "Chelsea", "Barcelona", "Bayern Munich",
        "Juventus", "Tottenham", "Liverpool", "Basel", "PSG", "Real Madrid",
        "Porto", "Besiktas", "Sevilla", "Manchester United", "Roma", "Shaktar Donetsk" };
List<String> teamList = Arrays.asList(teams);
Scanner sc = new Scanner(System.in);
String winner;
do {
    System.out.print("\nWho do you think will win?: ");
    winner = sc.nextLine();
} while (!teamList.stream().anyMatch(winner::equalsIgnoreCase));
System.out.printf("You like %s?%n", winner);
Predicate<String> predicate = (Predicate<String>) winner::equalsIgnoreCase;
System.out.printf("I think %s will knock them out this year%n", teamList.stream()
        .filter(predicate.negate()).collect(Collectors.joining(" or ")));

示例運行:

Who do you think will win?: basel
You like basel?
I think Manchester City or Chelsea or Barcelona or Bayern Munich or Juventus or Tottenham or Liverpool or PSG or Real Madrid or Porto or Besiktas or Sevilla or Manchester United or Roma or Shaktar Donetsk will knock them out this year

暫無
暫無

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

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