java - 这个错误代码是什么意思?线程“主”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;
}
}
}
我需要为最终产品打印帐号、服务类型,并根据服务类型、使用的分钟数或白天和夜间分钟数。然后根据答案打印总账单。
解决方案
发生这种情况是因为您将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);
推荐阅读
- c# - 使用位置记录构造函数和/或初始化属性时如何检查参数是否为空?
- node.js - 为什么节点 14 和 16 的 AzureDevOps 私有 NPM 存储库如此缓慢?
- python - 将一个列表中的值(在 dict 中)与另一个列表进行比较 [Python]
- linux - 如何在 UNIX/LINUX 中特定行的末尾添加字符?
- c# - 无法使用凭证从 C# 控制台应用程序连接到 IBM MQ
- javascript - 推杆在 30 秒后断开
- css - prefers-color-scheme:dark 不会从 SCSS 编译为 CSS 样式表
- haskell - 我可以教 GHC 数学归纳法吗?
- php - Wordpress 中 CPT 的分层分类和永久链接返回 404
- r - 带有ggplot的for循环-rep_len(TRUE,nrow(x))中的错误:无效的'length.out'值