我希望能够输入:
Avoid:
alice bob carol dave eve
并且在arraylist中避免使用{alice,bob,dave,eve}
package lunch;
import java.util.ArrayList;
import java.util.Scanner;
public class LetsDoLunch {
public static void main(String[] args){
String s = "";
ArrayList<String> avoid = new ArrayList<String>();
System.out.println("Avoid:");
Scanner sc2 = new Scanner(System.in);
while(true){
s = sc2.next();
if(s.equals("")){
break;}
avoid.add(s);
}
System.out.println("done");
for (int i = 0; i < avoid.size(); i++)
{
System.out.println(avoid.get(i));
}
我很确定它与sc2.next()有关; 我在避免之后进入输入行:然后即使按下回车
也没有任何反应答案 0 :(得分:1)
而不是s = sc2.next()使用s = sc2.nextLine()
答案 1 :(得分:0)
试试这个
public static void main(String[] args) {
String s = "";
ArrayList<String> avoid = new ArrayList<String>();
System.out.println("Avoid:");
Scanner sc2 = new Scanner(System.in);
Boolean flag = true;
s = sc2.nextLine();
String[] words = s.split("\\s+");
for (int i = 0; i < words.length; i++) {
avoid.add(words[i]);
}
System.out.println("done");
for (int i = 0; i < avoid.size(); i++) {
System.out.println(avoid.get(i));
}
}