Java ATM程序超限提示功能有效性验证求助
你的ATM程序限额提示问题修正
首先,你的代码里存在几个会导致限额提示异常或功能错误的问题,下面逐一说明并给出修复后的完整代码:
主要问题分析
- 存款逻辑错误:你给存款操作加了
num > checking的判断,但存款是向账户存入资金,不存在"超出限额"的情况,这个判断完全多余,应该删除。 - 储蓄账户取款的致命错误:在储蓄账户取款分支中,
double total3 = cin.nextDouble();是错误的,这会让用户额外输入一次数值,正确的应该是计算savings - w2。 - 账户余额未更新:存款或取款后,你只是计算了新余额但没有修改
checking或savings变量,导致后续操作的余额始终是初始值,提示逻辑也会失效。 - Scanner输入残留问题:使用
nextDouble()后,输入流中会遗留换行符,导致下一次nextLine()读取空字符串,菜单选择会出现异常,需要在nextDouble()后调用cin.nextLine()消费换行。 - 余额查询逻辑错误:查询余额时用了未更新的
deposit和withdrawal变量,直接使用当前的checking或savings值即可。
修复后的完整代码
package atm; import java.util.Scanner; public class atm { public static void main(String[] args) { Scanner cin = new Scanner(System.in); // 初始化账户余额(改用double类型适配小数金额) double checking = 1000, savings = 5000; System.out.println("======================"); System.out.println("Welcome to Sound Bank!"); System.out.println("======================"); while (true) { System.out.println("Please select which options: \n (C)Checking Balance \n (S)Savings"); String choice1 = cin.nextLine(); System.out.println("Please select of the following: "); System.out.println("(D)Deposit \n (W)Withdrawal \n (B)Check Balance \n (E)Exit"); String choice2 = cin.nextLine(); switch(choice2.toUpperCase()) { case "D": if (choice1.equalsIgnoreCase("C")) { System.out.println("Enter your deposit amount:"); double num = cin.nextDouble(); // 消费nextDouble留下的换行符,避免后续nextLine读取空内容 cin.nextLine(); checking += num; System.out.println("You entered " + num); System.out.println("You have " + checking); } else if (choice1.equalsIgnoreCase("S")) { System.out.println("Enter your deposit amount"); double num1 = cin.nextDouble(); cin.nextLine(); savings += num1; System.out.println("You entered " + num1); System.out.println("You have " + savings); } break; case "W": if (choice1.equalsIgnoreCase("C")) { System.out.println("Enter your withdrawal amount:"); double w = cin.nextDouble(); cin.nextLine(); // 先判断是否超限额,超了只提示,不执行扣款 if(w > checking) { System.out.println("You are over the limit."); } else { checking -= w; System.out.println("You entered " + w); System.out.println("You have " + checking); } } else if (choice1.equalsIgnoreCase("S")) { System.out.println("Enter your withdrawal amount:"); double w2 = cin.nextDouble(); cin.nextLine(); if(w2 > savings) { System.out.println("You are over the limit."); } else { savings -= w2; System.out.println("You entered " + w2); System.out.println("You have " + savings); } } break; case "B": if (choice1.equalsIgnoreCase("C")) { System.out.println("Your total amount is: " + checking); } else if (choice1.equalsIgnoreCase("S")) { System.out.println("Your total amount is: " + savings); } break; case "E": System.exit(0); break; default: System.out.println("Invalid option, please try again."); } } } }
关键修改说明
- 移除存款的限额判断:存款无需判断金额是否超过余额,直接累加即可。
- 修正储蓄账户取款的余额计算:将错误的
cin.nextDouble()改为savings -= w2,并在判断通过后更新余额。 - 实时更新账户余额:存款和取款操作后,直接修改
checking或savings的值,确保后续操作使用最新余额。 - 处理Scanner输入残留:每次
nextDouble()后添加cin.nextLine(),避免下一次nextLine()读取空字符串。 - 优化取款逻辑:先判断取款金额是否超过余额,若超限额则只提示,不执行扣款操作;若正常则扣款并显示新余额。
- 简化余额查询:直接输出当前的
checking或savings值,无需使用未更新的中间变量。
现在运行修复后的代码,当你取款金额超过对应账户余额时,就会正确触发"You are over the limit"的提示,同时其他功能也能正常工作。
内容的提问来源于stack exchange,提问作者River Yoo
相关产品推荐
相关产品推荐

