我正在创建一个带有用户在控制台中输入的字符串的数组列表。问题是,当他们输入超过 2 个单词的字符串时,程序无法按预期运行。
这是我到目前为止所拥有的:
ArrayList<String> list = new ArrayList<String>();
Scanner scan = new Scanner (System.in);
int i;
System.out.print("How many TV shows do you hope to watch this week? ");
i = scan.nextInt();
scan.nextLine();
for(int j = 0; j<i; j++){
System.out.print("Enter show " + (j+1) + ": ");
list.add(scan.nextLine());
}
System.out.print("Have you caught up to any shows (answer yes or no): ");
while (scan.nextLine().equalsIgnoreCase("yes")){
System.out.print("Which show? ");
String show = new String(scan.nextLine());
if(list.contains(show)){
list.remove(list.indexOf(show));
}else {
System.out.print("That show is not on original list!");
}
}
System.out.println("Here's what you still have to watch this week:");
System.out.println(list);
顺便说一句,我尝试从 next() 更改为 nextLine() 它仍然无法按预期工作。
我的预期输出是:
示例 1:假设您还没有 catch 任何节目 (这有效,感谢下面用户的响应)
How many TV shows do you hope to watch this week? 3
Enter show 1: RWBY
Enter show 2: Kengan Ashura
Enter show 3: The Good Place
Have you caught up to any shows (answer yes or no): no
Here's what you still have to watch this week:
[RWBY, Kengan Ashura, The Good Place]
示例 2:假设您已追完一场节目并将更新列表 (不工作)
How many TV shows do you hope to watch this week? 3
Enter show 1: Father Brown
Enter show 2: Death in Paradise
Enter show 3: Watchmen
Have you caught up to any shows (answer yes or no): yes
Which show? Death in Paradise
Any other shows you're caught up with? (yes/no) no
Here's what you still have to watch this week:
[Father Brown, Watchmen]
示例 3:假设您已观看一场节目,但它不在列表中。 (它不会更新列表,因为将您正在观看的新节目添加到列表中只是将其删除是没有意义的。)
How many TV shows do you hope to watch this week? 2
Enter show 1: Watchmen
Enter show 2: RWBY
Have you caught up to any shows (answer yes or no): yes
Which show? Monday Night Football
That show is not on original list!
Any other shows you're caught up with? (yes/no) no
Here's what you still have to watch this week:
[Watchmen, RWBY]
谢谢!
最佳答案
以下是我如何获得您可能需要的内容:
Set<String> list = new HashSet<>();
Scanner scan = new Scanner (System.in);
System.out.print("How many TV shows do you hope to watch this week? ");
int i = Integer.parseInt(scan.nextLine());
for(int j = 0; j<i; j++){
System.out.print("Enter show " + (j+1) + ": ");
list.add(scan.nextLine());
}
System.out.print("Have you caught up to any shows (answer yes or no): ");
String showName;
String answer;
do {
answer = scan.nextLine();
if (answer.equalsIgnoreCase("no")) {
break;
} else {
System.out.print("Which show? ");
showName = scan.nextLine();
if(list.contains(showName)){
list.remove(showName);
} else {
System.out.println("That show is not on original list!");
}
System.out.println("Any other shows you're caught up with? (yes/no): "); // What yo do if I don't
// enter a "yes" or a "no"?
}
} while(answer.equalsIgnoreCase("yes"));
System.out.println("Here's what you still have to watch this week:");
System.out.println(list);
- 首先,我会建议您使用
Set<String>
而不是List<String>
正如我在代码中所做的那样。 - 其次,考虑用户可以在哪些位置输入不应该输入的值以及您希望如何处理它们。比如,当你要输入观看的节目数时,如果我输入一个单词,就会抛出异常,如何处理?如果我输入"is"或“否”以外的任何内容,如何处理这种情况?
关于java - 如何扫描循环内的行并将其插入到ArrayList中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58738717/