繁体   English   中英

如何在Java中的ArrayList中扫描多个单词

[英]how to scan more than one word in ArrayList in java

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());
    }

}

但不会扫描多个字

我该怎么办?

使用Loop进行输入和多次存储

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());
    }
}

尝试这样的事情:

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,它将停止循环

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM