java - 有人可以看看我的 atm 项目代码吗?
问题描述
目前,我正在做一个 ATM 项目。我能够让它工作。但是,如果用户在 switch 语句中输入小写字母而不是大写字母,我不确定该怎么做。另外,我是否应该使用 while 循环,以便用户能够重复这些步骤。最后,我坚持如何让用户随时退出程序。例如,完成他们的选择后退出。
public static void main(String[] args) {
double checkingAccount = 5000;
double savingAccount = 2000;
Scanner cin = new Scanner(System.in);
//Welcoming the user!
System.out.println("=======================");
System.out.println("Welcome to SoundBank!");
System.out.println("=======================");
// Selections for the user
System.out.println("Please choose: \nA - Checking Account \nB - Savings Account");
String choice1 = cin.nextLine();
System.out.println("Please choose of the following: \nD - Deposit \nW - Withdrawal \nC - Check Balance \nE - Exit");
String choice2 = cin.nextLine();
// It's time for loops
switch (choice2) {
case "D":
// Checking Account
if (choice1.equalsIgnoreCase("A")) {
System.out.println("How much you do you want to deposit?");
double amount = cin.nextDouble();
double total1 = checkingAccount + amount;
System.out.println("You deposited " + amount);
System.out.println("You currently have: " + total1);
} else
// Saving Account
if (choice1.equalsIgnoreCase("B")) {
System.out.println("How much you do you want to deposit?");
double amount2 = cin.nextDouble();
double total2 = savingAccount + amount2;
System.out.println("You deposited " + amount2);
System.out.println("You currently have: " + total2);
}
break;
case "W":
// Checking Account
if (choice1.equalsIgnoreCase("A")) {
System.out.println("How much do you want to withdraw?");
double amount3 = cin.nextDouble();
double total3 = checkingAccount - amount3;
System.out.println("You withdrew " + amount3);
System.out.println("You currently have: " + total3);
} else
// Saving Account
if (choice1.equalsIgnoreCase("B")) {
System.out.println("How much do you want to withdraw?");
double amount4 = cin.nextDouble();
double total4 = savingAccount - amount4;
System.out.println("You withdrew " + amount4);
System.out.println("You currently have: " + total4);
}
break;
case "C":
// Checking Account
if (choice1.equalsIgnoreCase("A")) {
System.out.println("Your current balance is: " + checkingAccount);
} else
// Saving Account
if (choice1.equalsIgnoreCase("B")) {
System.out.println("Your current balance is: " + savingAccount);
}
break;
case "E":
System.out.println("You exited the program.");
break;
default:
System.out.println("Please choose a selction.");
}
}
解决方案
我会做
switch(choice2.toUpperCase())
所以你不必担心它们是大写还是小写,但你也可以这样做
case "D": // fall through
case "d":
同一代码块有两种情况。
对于已有的程序,既然都在main
函数里面,直接return即可
case "E":
System.out.println("You exited the program.");
return;
推荐阅读
- java - 如何仅用分钟格式化 LocalDateTime(截断秒)?
- caching - 如何使用 apollo 客户端处理在 graphql 的子查询中为嵌套对象返回不同值的查询
- python - Python:遍历列表给出语法错误
- python - 将字符串转换为numpy数组python
- python - 调用 getter 并写入 txt 文件
- c# - 通过 IP 地址连接时出现 OWIN 错误
- docker - 无法配置新的 dunglas/mercure 映像
- docker - 使用 GPUS 和 systemctl 运行 docker nvidia 容器
- linux - 如何使用 bash 脚本通过比较文件夹名称和系统时间戳作为参考来删除多个文件夹?
- laravel - 检查两个数据库表的数组结果是否不匹配