I have to make a loop taking a users input until “done” is entered

前端 未结 4 1214
被撕碎了的回忆
被撕碎了的回忆 2020-12-20 01:53

I\'m trying to make an ArrayList that takes in multiple names that the user enters, until the word done is inserted but I\'m not really sure how.

4条回答
  •  感情败类
    2020-12-20 02:12

    ArrayList names = new ArrayList();
    String userInput;
    Scanner scanner = new Scanner(System.in);
    while (true) {
        userInput = scanner.next();
        if (userInput.equals("done")) {
            break;
        } else {
            names.add(userInput);
        }
    }       
    scanner.close();
    

提交回复
热议问题