首页 > 解决方案 > 这个错误代码是什么意思?线程“主”java.util.InputMismatchException 中的异常

问题描述

我的 java 代码不能正常工作。在它要求用户输入 R 或 P 后,我不断收到此错误消息。这是什么意思?我该如何解决?

Exception in thread "main" java.util.InputMismatchException
    at java.base/java.util.Scanner.throwFor(Scanner.java:939)
    at java.base/java.util.Scanner.next(Scanner.java:1594)
    at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
    at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
    at PhoneBill3.main(PhoneBill3.java:17)

import java.util.Scanner;
public class PhoneBill3
{
   public static void main (String [] args)
   {
      double acctNum;
      int svcType=0;
      int dtmin=0;
      int ntmin=0;
      int dtFree=50;
      int ntFree=100;
      int minUsed=0;
      Scanner scan= new Scanner (System.in);
      System.out.print ("Please enter your account number: ");
      acctNum=scan.nextDouble();
      System.out.println ("Service type (R/P): ");
      svcType = scan.nextInt ();
      System.out.print("You entered " +svcType);

      //using switch to decide what to do with user input
      switch (svcType)
      {
         case 'R': 
         //if case R is entered, this should prompt the user to enter          
total minutes used and determin the cost of the Regular bill
            System.out.println ("Total minutes used: ");
            minUsed=scan.nextInt ();
            if (minUsed<50){
               System.out.println ("Account number: " + acctNum);
               System.out.println ("Account type: Regular");
               System.out.println ("Total minutes: " + minUsed);
               System.out.println ("Amount due: $15.00");}
            else{
               System.out.println ("Account number: " + acctNum);
               System.out.println ("Account type: Regular");
               System.out.println ("Total minutes: " + minUsed);
               System.out.println ("Amount due: $"+ 15 + (minUsed-    
50)*.2);}
            break;
         case 'P':
         //if case P is entered, this should prompt the user to enter     
day time and night time minutes used
            System.out.println ("Number of daytime minutes used: ");
            dtmin=scan.nextInt ();
            double dtTtlDue=0.00;
            System.out.println ("Number of nighttime minutes used: ");
            ntmin=scan.nextInt ();
            double ntTtlDue=0.00;
            dtTtlDue= (dtmin-dtFree)*.2;
            ntTtlDue= ((ntmin-ntFree)*.1);
            System.out.println ("Account number: " + acctNum);
            System.out.println ("Account type: Premium");
            System.out.println ("Daytime Min: "+ dtmin);
            System.out.println ("Nighttime Minutes: " + ntmin);
            System.out.println ("Amount due: $" + 25.00+ dtTtlDue + 
ntTtlDue);
            break;
       default:
            System.out.println ("That is not a valid service type. 
Enter R for regular or P for premium.");
            break;

       }

    }
}

我需要为最终产品打印帐号、服务类型,并根据服务类型、使用的分钟数或白天和夜间分钟数。然后根据答案打印总账单。

标签: javaerror-codeinputmismatchexception

解决方案


发生这种情况是因为您将R & P输入读取为int。您应该将其读取为String 请更改

System.out.println ("Service type (R/P): ");
svcType = scan.nextInt ();

System.out.println ("Service type (R/P): ");
svcType = scan.next ().charAt(0);

推荐阅读