在我的代碼中,ArrayList 將列印到控制臺。從那里,用戶將從串列中輸入一個名稱。它將檢查名稱到串列中。我的代碼檢查用戶輸入 3 次。我遇到的問題是有時它會正確地通過代碼,直到您輸入不正確的值。然后它就走到最后,它說“我在一切之外”。我只用它來查看我的代碼在哪里。我想知道我的代碼哪里出錯了。我怎樣才能解決這個問題?非常感謝任何幫助!
ArrayList<String> forwards = new ArrayList<String>();
forwards.add("Matthew Barzal");
forwards.add("Josh Bailey");
forwards.add("Anthony Beauvillier");
forwards.add("Kieffer Bellows");
forwards.add("Casey Cizikas");
forwards.add("Cal Clutterbuck");
forwards.add("Anders Lee");
forwards.add("Matt Martin");
forwards.add("Brock Nelson");
forwards.add("Oliver Wahlstrom");
forwards.add("Zach Parise");
forwards.add("Kyle Palmieri");
forwards.add("JG Pageau");
System.out.println("These are your starting forwards:");
for (int i = 0; i < forwards.size(); i ) {
System.out.println(forwards.get(i));
}
System.out.println();
System.out.println("Please pick three(3) forwards to be line in #1:");
Scanner input = new Scanner(System.in);
String userInput;
String userInput2 = "";
String userInput3 = "";
userInput = input.nextLine();
while (true) {
for(String i : forwards) {
if(userInput.equals(i)) {
System.out.println("Please pick another forward...");
System.out.println("I am in first if statement");
userInput2 = input.nextLine();
continue;
// return;
} else if (userInput2.equals(i)) {
System.out.println(userInput);
System.out.println("Please pick another forward...");
System.out.println("I am in second if statement");
userInput3 = input.nextLine();
//continue;
} else if (userInput3.equals(i)) {
System.out.println("I am in third if statement");
System.out.println("Your forwards for Line # 1 are: "
userInput " " userInput2 " " userInput3);
break;
//return;
}
}
System.out.println("I am outside everything");
System.out.print("Input not found in forwards list. Enter new value: \n");
userInput = input.nextLine();
continue;
}
uj5u.com熱心網友回復:
我使用 set 重構了您的代碼,使其更短更簡單。看看這個,看看它是否滿足您的需求。現在應該可以正常作業了。
Set<String> forwards = new HashSet<>();
forwards.add("Matthew Barzal");
forwards.add("Josh Bailey");
forwards.add("Anthony Beauvillier");
forwards.add("Kieffer Bellows");
forwards.add("Casey Cizikas");
forwards.add("Cal Clutterbuck");
forwards.add("Anders Lee");
forwards.add("Matt Martin");
forwards.add("Brock Nelson");
forwards.add("Oliver Wahlstrom");
forwards.add("Zach Parise");
forwards.add("Kyle Palmieri");
forwards.add("JG Pageau");
System.out.println("These are your starting forwards:");
for (String forward : forwards) {
System.out.println(forward);
}
System.out.println("Please pick three(3) forwards to be line in #1:");
Scanner input = new Scanner(System.in);
List<String> selectedForwards = new ArrayList<>();
String userInput;
while (selectedForwards.size() < 3) {
System.out.println("Select the next forward: \n");
userInput = input.nextLine();
if (!forwards.contains(userInput)) {
System.out.println("Input not found in forwards list. Enter new value: \n");
continue;
}
selectedForwards.add(userInput);
forwards.remove(userInput);
}
System.out.println(selectedForwards);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/343070.html
