public class WC {
private ArrayList<String> note;
public WC() {
note = new ArrayList<String>();
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
WC w1 = new WC();
w1.note.add(input.next());
}
}
但它不会扫描多个单词
我该怎么办?
答案 0 :(得分:0)
使用循环多次输入和存储
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
WC w1 = new WC();
// variable amount is number of times you want to enter data
int amount = 5;
for (int i = 1; i <= amount; i++) {
w1.note.add(input.next());
}
}
答案 1 :(得分:0)
尝试这样的事情:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
WC w1 = new WC();
String str;
while (!(str = input.next()).equals("quit")) {
w1.note.add(str);
}
}
这样,您只需键入quit即可停止循环