【问题标题】:Keep total from resetting on basic banking app防止在基本银行应用程序上重置总数
【发布时间】: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


    【解决方案1】:

    您在事件发生时调用您的函数(按钮单击)。现在,您的函数中的第一条语句将余额设置为 0。没有任何东西可以跟踪它。

    只需将语句移出,变量就会持续存在。

    let currentBalance = 0;
    
    function bankingApp() {
    
      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.");
      }
    }
    
    
    

    【讨论】:

    • 哇。这比我想象的要简单得多。谢谢!
    • 没问题。请接受(绿色勾号)/支持答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-06
    • 2010-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多