【问题标题】:Breaking up a single user input and storing it in two different variables. (Java)分解单个用户输入并将其存储在两个不同的变量中。 (爪哇)
【发布时间】:2015-02-13 05:58:54
【问题描述】:

我对编程非常陌生,尤其是 Java。我需要创建一个程序来计算餐厅每个条目的订单数量。餐厅有 3 个条目,汉堡包、沙拉和特色菜。

我需要设置我的程序,以便用户输入,比如“汉堡 3”,它会跟踪数字并在最后将其相加。如果用户输入“退出”,程序将退出。

System.out.println("Enter the type (special, salad, or hamburger) of entrée followed by the number, or quit to exit the program.");

我正在考虑使用 while 循环,将其设置为如果用户输入 != 为“退出”,那么它将运行。

对我来说困难的是我不知道如何让我的程序考虑到用户输入的两个不同部分,“汉堡包 3”并总结最后的数字部分。

最后,我想让它说“你今天卖了 X 个汉堡包、Y 个沙拉和 Z 个特色菜”。

我们将不胜感激。

【问题讨论】:

  • String#split 使用" " 作为令牌

标签: java


【解决方案1】:

您可能希望使用三个 int 变量作为订单数量的运行记录:

public class Restaurant {
    private int specials = 0;
    private int salads = 0;
    private int hamburger = 0;

然后您可以使用do-while 循环向用户请求信息...

String input = null;
do {
    //...
} while ("quite".equalsIgnoreCase(input));

现在,您需要某种方式来要求用户输入。为此,您可以轻松地使用java.util.Scanner。见the Scanning tutorial

Scanner scanner = new Scanner(System.in);
//...
do {
    System.out.println("Enter the type (special, salad, or hamburger) of entrée followed by the number, or quit to exit the program.");
    input = scanner.nextLine();

现在您有了来自用户的输入,您需要做出一些决定。您需要知道他们是否输入了有效的输入(主菜和金额)以及他们是否输入了可用选项...

// Break the input apart at the spaces...
String[] parts = input.split(" "); 
// We only care if there are two parts...
if (parts.length == 2) {
    // Process the parts...
} else if (parts.length == 0 || !"quite".equalsIgnoreCase(parts[0])) {
    System.out.println("Your selection is invalid");
}

好的,所以我们现在可以确定用户输入是否满足或不满足第一个要求([text][space][text]),现在我们需要确定这些值是否真的有效......

首先,让我们检查一下数量...

if (parts.length == 2) {
    // We user another Scanner, as this can determine if the String
    // is an `int` value (or at least starts with one)
    Scanner test = new Scanner(parts[1]);
    if (test.hasInt()) {
        int quantity = test.nextInt();
        // continue processing...
    } else {
        System.out.println(parts[1] + " is not a valid quantity");
    }

现在我们要检查是否实际输入了有效的主菜...

if (test.hasInt()) {
    int quantity = test.nextInt();
    // We could use a case statement here, but for simplicity...
    if ("special".equalsIgnoreCase(parts[0])) {
        specials += quantity;
    } else if ("salad".equalsIgnoreCase(parts[0])) {
        salads += quantity;
    } else if ("hamburger".equalsIgnoreCase(parts[0])) {
        hamburger += quantity;
    } else {
        System.out.println(parts[0] + " is not a valid entree");
    }

查看The if-then and if-then-else StatementsThe while and do-while Statements 了解更多详情。

您还可以找到Learning the Java Language 的一些帮助。另外,请保留一份JavaDocs 的副本,这样可以更轻松地在 API 中找到对类的引用

【讨论】:

  • 感谢您提供如此快速/详细的回答。太晚了,我明天试试。唯一的问题是我正在上课,我们还没有学到你解释的大部分方法。我认为老师希望我只使用 while 和 if 语句来完成所有这些......
  • 嗯,基本上,这就是全部...我想Scanner 是唯一复杂的部分;)
【解决方案2】:

这两种方法应该是你要找的。​​p>

用于拆分:String.split(String regex) http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split(java.lang.String)

用于将字符串解析为整数:Integer.parseInt(String s) http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#parseInt(java.lang.String)

【讨论】:

    【解决方案3】:

    您可以使用input.split(" ") 拆分字符串。此方法为您提供两个字符串 - 主字符串的两个部分。您使用 (" ") 拆分的字符将不再出现在字符串中。

    要从字符串中获取整数,可以使用静态方法Integer.parseInt(inputPartWithCount)

    我希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-07
      • 1970-01-01
      • 1970-01-01
      • 2020-02-21
      • 1970-01-01
      相关资源
      最近更新 更多