【问题标题】:How to stop scanner from accepting input如何阻止扫描仪接受输入
【发布时间】:2017-09-23 04:36:01
【问题描述】:

我正在编写非常简单的代码,它要求您输入您有多少钱以及您想购买哪些产品(一行)。然后该程序应该告诉您是否有足够的钱购买产品。此外,它应该打印价格最低的产品。

示例:

  1. 输入您拥有的金额:100
  2. 输入您要购买的产品和每个产品的价值:Apple 10Orange 20

  3. 代码输出:

    • you have enough money
    • the lowest price is (Apple 10)

这段代码有两个问题

  1. 首先,当我试图阻止scanner 接受输入时,我应该输入“停止”作为输入。但是,在我的情况下,只有当我输入“停止”2 次时才会执行该操作。我不知道为什么。

  2. 我需要确定最低产品价值并打印出来。我尝试了很多不同的方法,但没有一个对我有用。

这是我目前的代码:

Scanner input = new Scanner (System.in);

String productName="";
double totalPrice=0;
double productValue = 0;


System.out.println("How much money do you have? ");
double money = input.nextDouble();

System.out.println("please insert the items in the invoice (the name of product and its price): "
        + " insert \"stop\" as the name of the product to finish your input");


while (!(productName.equals("stop")) ){
    if(input.hasNext()){  productName = input.next();}
    if (input.hasNextDouble()){ productValue = input.nextDouble();}
    totalPrice = totalPrice + productValue;
}

if   (money > totalPrice ){    
    System.out.println("you have enough money");
} else {
    System.out.println("you don't have enough money");
}

【问题讨论】:

  • 请为您正在编程的语言添加标签。(这可能有助于您获得更多浏览量。)
  • 你有一个“停止”的错误,因为像 .hasNext* 这样的方法是阻塞方法。第一次输入“stop”后,程序会被 hasNextDouble 方法阻止,直到您输入内容。

标签: java if-statement while-loop minimum


【解决方案1】:

不要像现在这样解析用户输入,而是尝试一次解析整行,然后使用String.split()拆分输入字符串

还要考虑您第一次致电Scanner.nextDouble() 的真正目的是什么。它将读取用户的下一个双重输入,但不会读取到下一行(不会读取换行符)

【讨论】:

    【解决方案2】:

    您的代码在检查用户是否想要停止之前读取两个项目,这就是您必须提供两个输入的原因。要确定最小值,只需跟踪您目前看到的最小值以及与该值关联的名称。

        Scanner input = new Scanner (System.in);
    
        String productName="", minProductName = null;
        double totalPrice=0;
        double productValue = 0, minValue = -1;
    
    
        System.out.println("How much money do you have? ");
        double money = input.nextDouble();
    
        System.out.println("please insert the items in the invoice (the name of product and its price): "
                + " insert \"stop\" as the name of the product to finish your input");
    
    
        while (true){
            productName = input.next();
            if("stop".equals(productName))
                break;
            productValue = input.nextDouble();
            if(minValue < 0 || minValue > productValue){
                minValue = productValue;
                minProductName = productName;
            }
            totalPrice = totalPrice + productValue;
        }
    
        if   (money > totalPrice ){    
            System.out.println("you have enough money");
        } else {
            System.out.println("you don't have enough money");
        }
    
        System.out.println("Minimum product value: "+minProductName + " " +minValue);
    
        input.close();
    

    输入/输出:

    How much money do you have? 
    100
    please insert the items in the invoice (the name of product and its price):  insert "stop" as the name of the product to finish your input
    apple 10
    orange 5
    banana 50
    stop
    you have enough money
    Minimum product value: orange 5.0
    

    注意事项/注意事项:

    你可能会注意到这个条件被翻转了:

    if("stop".equals(productName))
    

    这是故意的,因为如果你有一个空的productName,那么如果你使用productName.equals(...),你的代码会抛出一个空指针,但是如果你使用像"stop"这样的常量,那么这不可能是空的,所以它永远不会抛出NullPointerException

    您永远不会验证您的输入 - 如果用户输入的值小于零怎么办?那有效吗?如果不是,那该怎么办?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-08-20
      • 2020-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-20
      相关资源
      最近更新 更多