【问题标题】:Print out an error when a decimal is entered in an int在 int 中输入小数时打印错误
【发布时间】:2014-10-09 03:49:03
【问题描述】:

我正在编写一个名为 Revenue 的程序,我已经满足了除一个之外的所有要求。

  System.out.print("\t Enter quantity(ies): ");
    quantity = keyboard.nextInt();
    if (quantity < 0 || quantity > 150 || quantity == '.'){

        System.out.println();
        System.out.println("\t Invalid item price.");
        System.out.println("\t Please run the program again");
        System.out.println();
        System.out.println("Thank you for using \"Temple\" store");
        System.exit(0);

我们必须向用户询问他们购买的商品的数量,并且不能有小数点或“。”在里面。例如,当要求用户输入他们想要购买的物品数量时,他们将输入数字。如果他们输入一个带小数的数字,它应该打印出来

     Invalid item price.
     Please run the program again

Thank you for using "Temple" store

【问题讨论】:

  • 你的教授会对此感到失望
  • @getlost 我认为 Kacey 想做一个 .contains('.'),但无论如何它都行不通,因为他的输入是 int 而不是 string

标签: java int java.util.scanner


【解决方案1】:

keyboard.nextInt() 不允许使用小数。如果您键入类似“3.14.15”的内容,它将引发异常。对于错误处理,您需要捕获此异常并打印出比默认情况下看到的堆栈跟踪更好的错误消息。

【讨论】:

  • @Kacey 你能用细节编辑你原来的问题吗?特别是,“如果我输入双精度”是什么意思?另外,你得到什么错误?
  • @Kacey 抱歉,我对Scanner.nextInt() 行为的猜测不正确。我已经编辑了我的答案,并提出了如何解决问题的建议。
  • 使用try-catch。捕获该异常并要求用户重试。
  • @theGreenCabbage 看起来 OP 应该只打印一条错误消息,例如“请再次运行程序”。
  • 为什么不在catch 中也加入Scanner?这样用户就不必再次运行程序。
【解决方案2】:

使用 try-catch 块来完成你想要的:

  System.out.print("\t Enter quantity(ies): ");
  try{
    quantity = keyboard.nextInt();
    if (quantity < 0 || quantity > 150)
     throw new IllegalArgumentException();
  } catch (IllegalArgumentException e) {
     System.out.println();
     System.out.println("\t Invalid item price.");
     System.out.println("\t Please run the program again");
     System.out.println();
     System.out.println("Thank you for using \"Temple\" store");
     System.exit(-1); //-1 signs an error to the application that launched the program
  }

【讨论】:

    【解决方案3】:

    使用下面的代码:

      String quantity = keyboard.next();  // use keyboard.next() instead of keyboard.nextInt() 
                     // because it will throw Exception if value is other 
                                           //than int   
    
       boolean flag = quantity.matches("[0-9]+");
        if (flag) {
            if (Integer.parseInt(quantity) < 0
                    || Integer.parseInt(quantity) > 150) {
                flag = false;
            }
        }
    
        if (!flag) {
            System.out.println();
            System.out.println("\t Invalid item price.");
            System.out.println("\t Please run the program again");
            System.out.println();
            System.out.println("Thank you for using \"Temple\" store");
            System.exit(0);
        }
    

    【讨论】:

    • 如果用户输入“abcde”会发生什么?
    • @Code-Apprentice : 谢谢我错过了检查字符串
    • “!@#$%”怎么样?我们可以继续前进。我认为一个更简单的解决方案是仍然调用nextInt(),它将处理许多错误情况(包括我们没有考虑过的情况)并捕获错误输入时抛出的异常。
    • @Code-Apprentice :我认为这只会过滤数字
    • 是的,看起来不错。注意你基本上是在重写Scanner.nextInt()Integer.parseInt()的逻辑。
    猜你喜欢
    • 2015-04-26
    • 1970-01-01
    • 2012-05-11
    • 1970-01-01
    • 2022-06-10
    • 1970-01-01
    • 2016-02-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多