Java账单计算程序遇java.util.InputMismatchException异常求助
修复Java账单计算程序的InputMismatchException问题
Hey,我来帮你搞定这个问题!你遇到的InputMismatchException是代码里一个典型的小失误,咱们一步步拆解修复:
核心错误原因
你用input.nextInt()来读取用户输入的套餐字母(A/B/C),但nextInt()只能处理整数类型的数据。当用户输入字母时,Scanner没办法把字母转换成整数,直接就抛出了这个异常。这是导致程序崩溃的根本原因。
具体修复步骤
1. 修正套餐输入的读取逻辑
首先,把存储套餐的变量类型从int改成char(因为要存字母),然后用正确的方式读取字符输入:
// 把变量类型从int改为char char subscription; // 读取用户输入的第一个字符(自动忽略空格/换行) subscription = input.next().charAt(0);
这里input.next()会读取用户输入的第一个字符串(直到空格为止),charAt(0)取这个字符串的第一个字符,这样就能正确获取A/B/C了。
2. 修复套餐时长的判断逻辑
看你代码里套餐A的判断:
if (TimeMin < 450) { AddTime = TimeMin - 450 ; Charges = AddTime * 0.45; // ... }
这完全搞反了!应该是当使用分钟数超过450时才计算额外费用,现在的逻辑是分钟数小于450时计算,会导致AddTime变成负数,额外费用也是负数,账单金额反而比基础套餐便宜,这明显不对。
同理,套餐B的判断也需要修正:
- 套餐A:把
if (TimeMin < 450)改成if (TimeMin > 450) - 套餐B:把
if (TimeMin < 900)改成if (TimeMin > 900)
修正后套餐A的逻辑还可以优化节省金额的提示:如果当前套餐更便宜,就告诉用户选其他套餐会多花多少钱,而不是显示负数的“节省”,更符合逻辑。
3. 其他小优化
- 变量名遵循Java驼峰命名法(比如
Subscription改成subscription,TimeMin改成timeMin),代码更易读 - 局部变量尽量在使用前声明,避免不必要的全局声明
- 初始化变量避免未赋值的编译警告
- 程序结束时关闭Scanner,避免资源泄漏
完整修复后的代码
import java.util.Scanner; public class Assignment2 { // 类名首字母大写,符合Java规范 public static void main(String[] args) { Scanner input = new Scanner(System.in); // 询问用户套餐类型 System.out.println("Please enter your subscription (A, B, or C) "); char subscription = input.next().charAt(0); switch (subscription) { case 'a': case 'A': System.out.println("Enter amount of minutes spent this month "); int timeMinA = input.nextInt(); double billA; if (timeMinA > 450) { double addTimeA = timeMinA - 450; double chargesA = addTimeA * 0.45; System.out.println("The extra charges are: $" + chargesA); billA = 39.99 + chargesA; System.out.println("Your bill this month will be: $" + billA); System.out.println(); // 计算与其他套餐的费用差异 double savingB = billA - 59.99; if (savingB > 0) { System.out.println("With package B you would have saved: $" + savingB); } else { System.out.println("With package B you would have spent $" + Math.abs(savingB) + " more"); } double savingC = billA - 69.99; if (savingC > 0) { System.out.println("With package C you would have saved: $" + savingC); } else { System.out.println("With package C you would have spent $" + Math.abs(savingC) + " more"); } } else { billA = 39.99; System.out.println("Your bill is $" + billA + " this month "); } break; case 'b': case 'B': System.out.println("Enter amount of minutes spent this month "); int timeMinB = input.nextInt(); double billB; if (timeMinB > 900) { double addTimeB = timeMinB - 900; double chargesB = addTimeB * 0.40; System.out.println("The extra charges are: $" + chargesB); billB = 59.99 + chargesB; System.out.println("Your bill this month will be: $" + billB); System.out.println(); double savingC = billB - 69.99; if (savingC > 0) { System.out.println("With package C you would have saved: $" + savingC); } else { System.out.println("With package C you would have spent $" + Math.abs(savingC) + " more"); } } else { billB = 59.99; System.out.println("Your bill is $" + billB + " this month "); } break; case 'c': case 'C': System.out.println("Enter amount of minutes spent this month "); 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.close(); // 关闭Scanner,避免资源泄漏 } }
测试验证
现在运行程序:
- 输入
A,再输入500,程序会计算额外50分钟的费用(50*0.45=22.5),总账单62.49,然后提示选B会多花2.5美元,选C会多花7.5美元,逻辑正确。 - 输入
B,再输入800,直接显示账单59.99,提示选C会多花10美元。 - 输入
C,任意分钟数都显示69.99。 - 输入
X,会提示无效输入。
这样就彻底解决了你的异常和逻辑错误啦!
内容的提问来源于stack exchange,提问作者s u
相关产品推荐
相关产品推荐

