0
我有添加字符串到arraylist的簡單代碼。我需要終止我的while循環時,用戶提供一個空白輸入(或點擊輸入兩次基本上)。按回車鍵退出(Java)
下面是代碼:
static ArrayList<String> names = new ArrayList();
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter name:");
String name = sc.nextLine();
while (!name.equals(" ")) { // ?
names.add(name);
name = sc.nextLine();
if (names.contains(name)) {
System.out.println(name + " already exists!");
name = sc.nextLine();
}
}
System.out.println(names);
}
' 「 」'是不一樣的'「」'! – ParkerHalo