繁体   English   中英

如何阻止扫描仪接受输入

[英]How to stop scanner from accepting input

我正在编写非常简单的代码,要求您输入您拥有的金额以及您希望购买的产品(在一条线上)。 然后该程序会告诉您是否有足够的钱购买产品。 此外,它应该以最低的价格打印产品。

例:

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

  3. 输出代码:

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

我有这个代码的2个问题

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

而不是按照现在的方式解析用户输入,尝试一次解析整行,然后使用String.split()拆分输入字符串

还要考虑你对Scanner.nextDouble()第一次调用是什么。 它将读取用户的下一个双输入但不会读到下一行(不会读取换行符)

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM