首页 > 解决方案 > 如何在用户输入完成之前继续输入字符串

问题描述

JAVAHey,所以我正在尝试为用户输入想要或(不想要)的一些“成分”创建一个数组列表,以便他们可以选择尽可能多的成分或不选择成分

输入是字符串,在客户选择完成分后,他们可以输入“完成”

所以我想要的是在数组列表中存储客户想要的尽可能多的成分,当他们输入完成时,程序结束

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;

                 }

标签: javaarraylist

解决方案


在我看来,最好从菜单中选择项目并仅输入菜单项字母或菜单项编号,而不是像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)。

即使选择了“完成”也允许用户添加到选择中也没有什么坏处,例如:

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

推荐阅读