java - 如何在java中多次检查来自ArrayList的用户输入
问题描述
在我的代码中,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;
}
解决方案
我使用集合将您的代码重构为更短更简单。看看这个,看看它是否满足您的需求。它现在应该可以正常工作了。
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);
}
推荐阅读
- java - 如何通过java中的另一个列表对列表对象进行排序
- xml - 如何避免文件末尾的新行 - xml to csv through xslt
- android - "detail":"JSON 解析错误 - 期望值:第 1 行第 1 列 (char 0) 颤振补丁请求
- mysql - AWS EBS django 无法部署并出现迁移错误
- c++ - 为什么如果我插入 1,2,2 作为输入,结果将是 0 而不是 1
- c# - Unity地形对撞机间歇性丢失碰撞
- sql - 将 ID 重新颁发给具有重复项的记录
- sql - SQL中属于两个不同列的两个不同行的数据之间的差异
- vba - 我在向 Google 日历中添加新活动时遇到问题
- reactjs - saga api调用成功后如何重定向