我必须让用户输入一个循环,直到“完成"被输入 [英] I have to make a loop taking a users input until "done" is entered

查看:17
本文介绍了我必须让用户输入一个循环,直到“完成"被输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个 ArrayList,它接受用户输入的多个名称,直到插入单词 done 但我不确定如何.如何实现?

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. How to achieve that?

推荐答案

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

这篇关于我必须让用户输入一个循环,直到“完成"被输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆