Java 如何在用户输入完之前一直输入字符串
JAVAHey所以我试图为用户输入想要或(不想要)的一些“成分”创建一个arraylist 因此,他们可以选择尽可能多的成分或不选择任何成分 输入是字符串,客户选择完配料后,可以键入“done” 因此,我想要的是在arraylist中存储客户想要的任意数量的配料,当他们键入“完成”后,程序就会结束Java 如何在用户输入完之前一直输入字符串,java,arraylist,Java,Arraylist,JAVAHey所以我试图为用户输入想要或(不想要)的一些“成分”创建一个arraylist 因此,他们可以选择尽可能多的成分或不选择任何成分 输入是字符串,客户选择完配料后,可以键入“done” 因此,我想要的是在arraylist中存储客户想要的任意数量的配料,当他们键入“完成”后,程序就会结束 Scanner cs = new Scanner(System.in); ArrayList<String> toppings = new ArrayList<String>
Scanner cs = new Scanner(System.in);
ArrayList<String> toppings = new ArrayList<String>();
System.out.println("What kind of toppings would you like? Avocado Bacon Cheese Pickles(when done type 'done'");
while(cs.hasNext()) {
toppings.add(cs.next());
String input = cs.nextLine();
if(input.equalsIgnoreCase("done"))
break;
}
Scanner cs=新扫描仪(System.in);
ArrayList toppings=新的ArrayList();
System.out.println(“你想要什么样的配料?鳄梨培根奶酪泡菜(完成后键入“完成”);
while(cs.hasNext()){
添加(cs.next());
字符串输入=cs.nextLine();
if(input.equalsIgnoreCase(“完成”))
打破
}
以下是我的做法:
Scanner cs = new Scanner(System.in);
ArrayList<String> toppings = new ArrayList<String>();
System.out.println("What kind of toppings would you like? Avocado Bacon Cheese Pickles(when done type 'done'");
String input = cs.nextLine();
while(!input.equals("done")) {
toppings.add(input);
input = cs.nextLine();
}
Scanner cs=新扫描仪(System.in);
ArrayList toppings=新的ArrayList();
System.out.println(“你想要什么样的配料?鳄梨培根奶酪泡菜(完成后键入“完成”);
字符串输入=cs.nextLine();
而(!input.equals(“done”)){
浇头。添加(输入);
input=cs.nextLine();
}
以下是您需要在我们的代码中进行的修复
Scanner cs = new Scanner( System.in );
ArrayList<String> toppings = new ArrayList<String>();
System.out.println( "What kind of toppings would you like? Avocado Bacon Cheese Pickles(when done type 'done'" );
String nextLine = cs.nextLine();
if( !nextLine.equalsIgnoreCase( "done" ) )
{
toppings.add( nextLine );
while( true )
{
nextLine = cs.nextLine();
if( nextLine.equalsIgnoreCase( "done" ) )
{
break;
}
toppings.add( nextLine );
}
}
System.out.println( toppings );
Scanner cs=新扫描仪(System.in);
ArrayList toppings=新的ArrayList();
System.out.println(“你想要什么样的配料?鳄梨培根奶酪泡菜(完成后键入“完成”);
字符串nextLine=cs.nextLine();
如果(!nextLine.equalsIgnoreCase(“完成”))
{
浇头。添加(下一行);
while(true)
{
nextLine=cs.nextLine();
如果(下一行等信号情况(“完成”))
{
打破
}
浇头。添加(下一行);
}
}
系统输出打印LN(浇头);
Scanner Scanner=新的扫描仪(System.in);
列表顶部=新的ArrayList();
字符串资源=null;
while(true){
System.out.println(“输入资源:”);
resource=scanner.nextLine();
if(resource.equalsIgnoreCase(“完成”)){
打破
}否则{
添加(资源);
}
}
系统输出打印LN(浇头);
在我看来,最好是从菜单中选择项目,然后只输入一个菜单项目字母或菜单项目编号,而不是输入整个单词,如Avocado,因为这极易出现打字错误,因此增加了输入验证的需要。菜单选择可能如下所示:
What kind of toppings would you like?
1) Avacodo 2) Bacon
3) Cheese 4) Pickels
5) Tomato 6) Lettuce
7) Onions 8) Done
Select Topping --> |
现在,用户只需输入一个菜单项编号,而不是输入所需的浇头。通过这种方式,您只需验证是否确实提供了一个编号,并且该编号实际上在菜单项范围内(例如1到8)
即使选择了Done,允许用户添加到选择中也不会有任何影响,例如:
What kind of toppings would you like?
1) Avacodo 2) Bacon
3) Cheese 4) Pickels
5) Tomato 6) Lettuce
7) Onions 8) Done
Select Topping --> 8
The toppings you selected are:
Pickels Tomato
Do you want to (A)dd a topping or (C)ontinue? -->
下面是一些将其付诸实践的示例代码:
Scanner cs = new Scanner(System.in);
String[] variousToppings = {"Avacodo", "Bacon", "Cheese", "Pickels",
"Tomato", "Lettuce", "Onions", "Done"};
ArrayList<String> desiredToppings = new ArrayList<>();
int choice = 0;
while (choice == 0) {
System.out.println();
System.out.println("What kind of toppings would you like?");
for (int i = 0; i < variousToppings.length; i++) {
System.out.printf("%-15s", i + 1 + ") " + variousToppings[i]);
if (i % 2 != 0 || i == (variousToppings.length - 1)) {
System.out.println();
}
}
System.out.print("Select Topping --> ");
String selection = cs.nextLine();
if (!selection.matches("\\d") || Integer.parseInt(selection) < 1
|| Integer.parseInt(selection) > variousToppings.length) {
System.err.println("Invalid Entry! Select a topping choice from 1 to "
+ variousToppings.length + "!");
continue;
}
choice = Integer.parseInt(selection);
if (choice == variousToppings.length) {
// 'Done' was selected!
System.out.println();
System.out.println("The toppings you selected are:");
for (int j = 0; j < desiredToppings.size(); j++) {
System.out.printf("%-15s", desiredToppings.get(j));
if (j % 2 != 0) {
System.out.println();
}
}
selection = "";
while (selection.equals("")) {
System.out.println();
System.out.print("Do you want to (A)dd a topping or (C)ontinue? --> ");
selection = cs.nextLine();
if (!selection.matches("(?i)[ac]")) {
System.err.println("Invalid Entry (" + selection + ")! Enter either A or C.");
selection = "";
}
}
if (selection.equalsIgnoreCase("c")) {
break;
}
}
if (desiredToppings.contains(variousToppings[choice - 1])) {
System.err.println("You have already selected that topping! "
+ "(" + variousToppings[choice - 1] + ")");
choice = 0;
continue;
}
if (!variousToppings[choice - 1].equalsIgnoreCase("done")){
desiredToppings.add(variousToppings[choice - 1]);
}
choice = 0;
}
Scanner cs=新扫描仪(System.in);
String[]variousToppings={“Avacodo”、“Bacon”、“Cheese”、“Pickels”,
“番茄”、“莴苣”、“洋葱”、“全熟”};
ArrayList desiredToppings=新的ArrayList();
int-choice=0;
while(选项==0){
System.out.println();
System.out.println(“您想要什么样的浇头?”);
对于(int i=0;i”);
字符串选择=cs.nextLine();
如果(!selection.matches(“\\d”)| | Integer.parseInt(selection)<1
||整数.parseInt(选择)>variousToppings.length){
System.err.println(“无效条目!从1到中选择一个顶部选项”
+各种浇头。长度+“!”;
继续;
}
choice=Integer.parseInt(选择);
如果(选项==各种浇头.长度){
//已选择“完成”!
System.out.println();
System.out.println(“您选择的配料是:”);
对于(int j=0;j”);
selection=cs.nextLine();
如果(!selection.matches(“(?i)[ac]”){
System.err.println(“无效条目(“+selection+”)!输入A或C.”;
选择=”;
}
}
if(选择相等信号案例(“c”)){
打破
}
}
如果(所需的浇头.包含(各种浇头[选项-1])){
System.err.println(“您已经选择了该浇头!”
+(“+多种配料[选择-1]+”);
选择=0;
继续;
}
如果(!variousToppings[choice-1].equalsIgnoreCase(“完成”)){
需要的浇头。添加(各种浇头[choice-1]);
}
选择=0;
}
Java 9+:List toppings=new BufferedReader(new InputStreamReader(System.in)).lines().takeWhile(i->!i.equalsIgnoreCase(“done”).collect(Collectors.toList());
Scanner cs = new Scanner(System.in);
String[] variousToppings = {"Avacodo", "Bacon", "Cheese", "Pickels",
"Tomato", "Lettuce", "Onions", "Done"};
ArrayList<String> desiredToppings = new ArrayList<>();
int choice = 0;
while (choice == 0) {
System.out.println();
System.out.println("What kind of toppings would you like?");
for (int i = 0; i < variousToppings.length; i++) {
System.out.printf("%-15s", i + 1 + ") " + variousToppings[i]);
if (i % 2 != 0 || i == (variousToppings.length - 1)) {
System.out.println();
}
}
System.out.print("Select Topping --> ");
String selection = cs.nextLine();
if (!selection.matches("\\d") || Integer.parseInt(selection) < 1
|| Integer.parseInt(selection) > variousToppings.length) {
System.err.println("Invalid Entry! Select a topping choice from 1 to "
+ variousToppings.length + "!");
continue;
}
choice = Integer.parseInt(selection);
if (choice == variousToppings.length) {
// 'Done' was selected!
System.out.println();
System.out.println("The toppings you selected are:");
for (int j = 0; j < desiredToppings.size(); j++) {
System.out.printf("%-15s", desiredToppings.get(j));
if (j % 2 != 0) {
System.out.println();
}
}
selection = "";
while (selection.equals("")) {
System.out.println();
System.out.print("Do you want to (A)dd a topping or (C)ontinue? --> ");
selection = cs.nextLine();
if (!selection.matches("(?i)[ac]")) {
System.err.println("Invalid Entry (" + selection + ")! Enter either A or C.");
selection = "";
}
}
if (selection.equalsIgnoreCase("c")) {
break;
}
}
if (desiredToppings.contains(variousToppings[choice - 1])) {
System.err.println("You have already selected that topping! "
+ "(" + variousToppings[choice - 1] + ")");
choice = 0;
continue;
}
if (!variousToppings[choice - 1].equalsIgnoreCase("done")){
desiredToppings.add(variousToppings[choice - 1]);
}
choice = 0;
}