【问题标题】:Java 'java.util.InputMismatchException' error when compiling编译时出现 Java 'java.util.InputMismatchException' 错误
【发布时间】:2018-06-01 20:18:03
【问题描述】:

我对 Java 还是很陌生。我有这个任务,我应该编写一个计算客户每月账单的程序。它应该要求用户输入客户购买的包裹的字母(A、B 或 C)以及使用的分钟数。然后它应该会显示总费用。

规格如下:

互联网服务提供商为其客户提供三种不同的订阅套餐:

套餐 A:每月 39.99 美元,提供 450 分钟的访问权限。额外分钟为每分钟 0.45 美元。套餐 B:每月 59.99 美元,提供 900 分钟的访问权限。额外小时为每分钟 0.40 美元。套餐 C:每月 69.99 美元,提供无限制访问。

这是我的代码:

 import java.util.Scanner;
    
    public class assignment2
    {
    
        public static void main(String[] args)
        {
    //declaration section 
    
    double AddTime;
    double Charges;
    double Saving;
    double Bill;
    int Subscription;
    int TimeMin;
    
    Scanner input = new Scanner (System.in);
    //ask user subscription
    System.out.println("Please enter you subscription (A,B, or C) ");
    Subscription = input.nextInt ();
    
    //if user choses subscription A
     switch (Subscription)
    {
            case 'a':
                    case 'A':
    System.out.println("Enter amount of minutes spent this month ");
    TimeMin = input.nextInt ();
        if (TimeMin < 450)
        {
        AddTime = TimeMin - 450 ;
        Charges = AddTime * 0.45;
        System.out.println("The charges are: $" + Charges);
        Bill = 39.99 + Charges;
        System.out.println("Your bill this month will be: $" + Bill);
        System.out.println(" ");
    //mention the user the savings with other subscriptions
    Saving = Bill - 59.99;
    System.out.println("With package B you would have saved: $" + Saving);
    Saving = Bill - 69.99;
    System.out.println("With package C you would have saved: $" + Saving);
    
        }
        else
        {System.out.println("Your bill is $39.99 this month ");}
                              break;
                    case 'b':
                    case 'B':
    // ask user for the amount of minutes used
    System.out.println("Enter amount of minutes spent this month ");
    TimeMin = input.nextInt();
        if (TimeMin < 900)
        {
        AddTime = TimeMin - 900;
        Charges = AddTime * 0.40;
        System.out.println("The charges are: $" + Charges);
        Bill = 59.99 + Charges;
        System.out.println("Your bill this month will be: $" + Bill);
        System.out.println(" ");
    //mention the user the savings with other subscriptions
    Saving = Bill - 69.99;
    System.out.println("With package C you would have saved: $" + Saving);
    
        }
        else
        {System.out.println("Your bill is $59.99 this month ");}
                break; 
    
                    case 'c':
                    case 'C':   
    // ask user for the amount of minutes used
        System.out.println("Enter amount of minutes spent this month ");
        TimeMin = input.nextInt();
        System.out.println("Your bill is $69.99 this month you have unlimited access to our service!");
    
    break;
    default:
    System.out.println("That is not A, B, or C!");  
    }
    
    System.out.println("Thanks for using our service!");
    }
           
    }

一旦我尝试编译它就会给我这个错误:

Exception in thread "main" java.util.InputMismatchException
        at java.util.Scanner.throwFor(Scanner.java:864)
        at java.util.Scanner.next(Scanner.java:1485)
        at java.util.Scanner.nextInt(Scanner.java:2117)
        at java.util.Scanner.nextInt(Scanner.java:2076)
        at assignment2.main(assignment2.java:21)

我还没有解决。知道如何修复我的代码吗?

【问题讨论】:

  • Javascript 不是 Java。请努力学习您尝试编写代码的语言的名称,并适当地标记您的问题。
  • System.out.println("Please enter you subscription (A,B, or C) "); 这看起来需要整数输入吗?如果没有,为什么要有Subscription = input.nextInt ();
  • Subscription = input.nextInt (); 表示用户应该键入数字。使用Scanner.next()
  • 附注:此“错误”不是标题所述的编译错误。这是一个运行时异常。由于编译器错误,程序甚至无法运行/编译。我想提到的第二件事是,您应该考虑让自己了解 Java 编码约定,例如 Google java Style Guide,以使您的代码对其他人更具可读性和可维护性。第三点:提问时要保持专业和客观。减少诸如“lol”、“nerds”之类的词。非常感谢。

标签: java debugging


【解决方案1】:

抛出异常的主要原因是您尝试从 InputStream(在您的情况下为 stdin / System.in)读取 整数值
但是,由于您提示用户输入 A、B 或 C,他很可能会输入这些字符之一。

根据文档Scanner#nextInt() 尝试将给定的 String 解析为整数。这意味着像“526”这样的字符串将返回一个整数 526。在这种情况下,A、B 或 C 应该代表什么?答案很简单。这些不是 0-9 范围内的值,因此在运行时会抛出 InputMismatchException 以指示无法解析输入。

解决此类问题的可能性有很多。为了保持您当前的开关盒结构,您可以使用以下内容:

public class Main
{
  public static void main( String[] args )
  {
    Scanner sc = new Scanner( System.in );

    System.out.println( "Enter A,B or C" );

    // read from console
    String rawInput = sc.nextLine();
    // remove unneccessary whitespaces.
    String trimmedInput = rawInput.trim();
    // assure that our string was not only containing whitespaces.
    if ( trimmedInput.length() > 0 )
    {
      // assign the value
      char value = trimmedInput.charAt( 0 );
      switchInputValue( value );
    }
  }

  private static void switchInputValue( char value )
  {
    switch ( value )
    {

      case 'a':
      case 'A':
        doSomethingForA();
        break;

      case 'b':
      case 'B':
        doSomethingForB();
        break;
      default:
        // get creative here
        // example:
        System.out.println("Something went wrong. - Wong input value.");
        break;

    }
  }
}

注意:从 Java 7 开始,我们可以直接切换字符串,但是如果您这样做不会对您的情况产生重大影响。为了完整起见,以下代码 sn-p 将与上面完全相同,但不会获得单个 char 值

public class Main
{
  public static void main( String[] args )
  {
    Scanner sc = new Scanner( System.in );
    System.out.println( "Enter A,B or C" );
    String rawInput = sc.nextLine();
    String trimmedInput = rawInput.trim();
    if ( trimmedInput.length() > 0 )
    {
      // switching our String
      switchInputValue( trimmedInput );
    }
  }

  private static void switchInputValue( String value )
  {
    switch ( value )
    {

      case "a":
      case "A":
        doSomethingForA();
        break;

      case "b":
      case "B":
        doSomethingForB();
        break;
      default:
        doSomethingIfEverythingElseFails();
        break;
    }
  }
}

正如您所见,除了参数的类型外,没有太大的区别,方法已更改为String value,并且案例用" "而不是' '标记。仍然因为您将单个字符作为输入除外,所以我会考虑第一种方法而不是第二种方法。

希望对您的问题有所帮助。

【讨论】:

  • 谢谢它真的帮助了我,我知道我的 stile 仍然如此 c++,但我无法掌握 Java...无论如何,再次感谢。
【解决方案2】:

您应该将input.nextInt(); 更改为input.nextLine();,将int Subscription 更改为String Subscription,并将switch case 中的任何' 更改为"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-29
    • 1970-01-01
    • 2020-12-18
    • 1970-01-01
    • 2016-10-04
    • 2017-01-25
    • 1970-01-01
    • 2023-04-11
    相关资源
    最近更新 更多