如何阻止扫描仪接受输入

时间:2017-04-26 00:13:33

标签: java if-statement while-loop minimum

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

示例:

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

  3. 代码输出:

    • you have enough money
    • the lowest price is (Apple 10)
  4. 此代码存在2个问题

    1. 首先,当我试图阻止scanner接受输入时,我应该进入"停止"作为输入。但是,在我的情况下,只有在我输入" stop" 2次。我不知道为什么。

    2. 我需要确定最小产品价值并打印出来。我尝试了很多不同的东西,但是没有一个能为我工作。

    3. 到目前为止,这是我的代码:

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

2 个答案:

答案 0 :(得分:0)

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

拆分输入字符串

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

答案 1 :(得分:0)

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

    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"之类的常量则没有办法这可以为null,因此它永远不会抛出NullPointerException

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

相关问题