这是什么错误代码是什么意思?异常线程“main” java.util.InputMismatchException

问题描述 投票:1回答:2

我的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;

       }

    }
}

我需要的最终产品打印的账户号码,业务类型,并根据服务类型,使用分钟或白天和夜间的分钟数。然后根据答案打印总法案将是什么。

java error-code inputmismatchexception
2个回答
0
投票

这是发生,因为你正在阅读的你R & P输入作为int.You应该读得String请更改

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

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

0
投票

您正在要求用户输入一个字符串值(长度为1的),并试图将其分配给一个int变量(svcType)。你需要更换2次:

1)改变svcType变量输入字符的类型:

char svcType = '?';

2)只提取来自用户的输入的输入的第一个字符:

scan.next().charAt(0);

你也可以toUpperCase()输入允许小写字母“R”或S':

scan.next().toUpperCase().charAt(0);
© www.soinside.com 2019 - 2024. All rights reserved.