【发布时间】:2021-09-12 17:29:06
【问题描述】:
我正在为初级 JS 课程开发一个基本的银行应用程序。
预期:
- 用户单击调用银行应用程序功能的按钮,使用 switch 语句选择提款、存款、检查余额或退出
- 选择提现或存款后,系统会提示他们添加金额
- 在控制台登录出入金金额和新余额
- 随后的每次按钮点击和提款/存款都应跟踪余额
实际:
- 由于 switch 语句,小计正在重置,我不知道如何在不删除中断的情况下保持运行总计。
有没有办法调整此代码以保持运行平衡?
function bankingApp() {
let currentBalance = 0;
let userPrompt = prompt(
"bank menu: w = withdrawal | d = deposit | b = balance | q = quit"
);
switch (userPrompt) {
case "w":
function withdrawFunds() {
let withdrawAmount = parseFloat(prompt("Withdraw amount: "));
currentBalance = currentBalance - withdrawAmount;
console.log(
"Withdraw: " + withdrawAmount + "New balance: " + currentBalance
);
}
withdrawFunds();
break;
case "d":
function depositFunds() {
let depositAmount = parseFloat(prompt("Deposit amount:"));
console.log(
"Deposit: " + depositAmount + "New balance: " + currentBalance
);
}
depositFunds();
break;
case "b":
function checkBalance() {
let balance = currentBalance;
console.log(balance);
}
checkBalance();
break;
case "q":
function quitProgram() {
let quit = "Quit the program.";
console.log(quit);
}
quitProgram();
break;
default:
console.log("That menu is not available.");
}
}
【问题讨论】:
标签: javascript switch-statement