Java';java.util.InputMismatchException';编译时出错

Java';java.util.InputMismatchException';编译时出错,java,debugging,Java,Debugging,我对Java很陌生。我有一个任务,我应该写一个程序来计算客户的每月账单。它应该要求用户输入客户购买的软件包的字母(A、B或C)以及使用的分钟数。然后它应该显示总费用 规格如下: 互联网服务提供商为其客户提供三种不同的订阅包: A包:每月39.99美元,提供450分钟的访问时间。额外分钟数为每分钟0.45美元。套餐B:每月59.99美元,提供900分钟的访问时间。额外工时为每分钟0.40美元。套餐C:每月69.99美元,提供无限制访问 这是我的密码: import java.util.Scann

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

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

你应该更改
input.nextInt()
to
input.nextLine()
int订阅
字符串订阅
以及
的任何
开关情况下
引发异常的主要原因是您试图从输入流读取整数值(在您的情况下为stdin/System.in)。
尽管您提示用户输入A、B或C,但很可能用户会输入这些字符之一

根据文档,试图将给定的字符串解析为整数。这意味着类似“526”的字符串“将返回一个整数526。在这种情况下,A、B或C代表什么?答案很简单。这些值不是0-9之间的值,因此在运行时会抛出一个
inputmaschException
,指示无法解析输入

解决这些问题有很多可能。要保持当前的开关盒结构,您可以采用以下方式:

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;

    }
  }
}

注意:由于Java7,我们可以直接切换字符串,但是如果您的情况没有太大区别的话。为了完成,以下代码段将执行与上面完全相同的操作,但不获取单个字符值:

正如您所看到的,除了参数od的类型之外,没有太大的区别。该方法已更改为
字符串值
,并且案例标记为
,而不是
。因为你除了一个字符外,我会考虑第一种方法,而不是第二种方法。
希望这对您的问题有所帮助。

Javascript不是Java。请努力学习您试图编写代码的语言名称,并适当标记您的问题。
System.out.println(“请输入您的订阅(A、B或C)”这看起来像是需要整数输入吗?如果没有,为什么有
Subscription=input.nextInt()
Subscription=input.nextInt()表示用户应键入数字。使用
Scanner.next()
。一些旁注:这个“错误”不是编译错误,正如标题所述。这是一个运行时异常。如果出现编译器错误,程序甚至无法运行/编译。我想提到的第二件事是,您应该考虑通知自己java代码约定,例如使代码更可重用和可维护。第三个注意事项:提问时要保持专业和客观。删掉“lol”、“书呆子”等词。非常感谢。谢谢它帮了我的忙,我知道我的门槛仍然如此C++但是我会得到java的诀窍…无论如何,再一次谢谢。
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;

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