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

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

问题描述

我试图使一个,它接受用户输入多个名称,直到字做的ArrayList 插入,但我真的不知道怎么样。如何做到这一点?


解决方案

 的ArrayList<串GT;名称=新的ArrayList<串GT;();
串userInput;
扫描仪扫描=新的扫描仪(System.in);
而(真){
    userInput = scanner.next();
    如果(userInput.equals(完成)){
        打破;
    }其他{
        names.add(userInput);
    }
}
scanner.close();

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

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

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