You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Node.js终端JavaScript取款机模拟器无输出退出问题求助

问题分析与代码修复

你的程序直接退出的核心原因是没有启动入口,代码里定义了所有函数,但从未调用初始启动函数。除此之外还有多处语法、API使用错误,以下是具体问题和修复后的代码:

主要问题点

  • 缺少程序启动逻辑:未调用cashMachineName()开启流程
  • 方法调用错误:所有questionInt未通过cashMachine对象调用(questionInt是readline-sync的成员方法)
  • 正则表达式错误:PIN验证正则/^[0-9]{4}£/要求输入带£,不符合实际PIN输入逻辑
  • 拼写错误:isNan→isNaN、selectFunction()→selectAccountType()、sufficent→sufficient、know→now
  • API误用:Node.js环境不支持alert,需替换为console.log
  • 逻辑错误:balance()用questionInt输出余额(应该用console.log)、密码验证的循环与递归混用导致重试逻辑混乱、exit()未真正终止程序

修复后的完整代码

const cashMachine = require('readline-sync');

let name,
  correct_pass = /^[0-9]{4}$/, // 修正正则:只匹配4位数字
  passTry = 3,
  currentBalance = 20000;

// 输入用户名
function cashMachineName() {
  name = cashMachine.question("Enter your name: ");
  if (name.trim() !== "") { // 优化空值判断,处理空格输入
    cashMachinePassword();
  } else {
    console.log("Name cannot be empty, please try again.");
    cashMachineName();
  }
}

// 输入密码
function cashMachinePassword() {
  let passwordEntry = cashMachine.questionInt(`Hello ${name}, please enter your 4-digit PIN: `);
  checkPassword(passwordEntry);
}

// 验证密码
function checkPassword(userInput) {
  if (correct_pass.test(userInput.toString())) { // 转字符串后匹配正则
    selectAccountType();
  } else {
    passTry -= 1;
    if (passTry === 0) {
      console.log("Incorrect PIN.");
      console.log("Maximum tries exceeded! Your account has been locked. Contact your bank for support.");
      exit();
    } else {
      console.log(`Incorrect PIN. Please try again. You have ${passTry} chance/s left.`);
      cashMachinePassword();
    }
  }
}

// 选择账户操作
function selectAccountType() {
  const accountType = cashMachine.questionInt(`Which operation would you like to perform? 
1. Check Balance 
2. Withdraw Cash 
3. Deposit Cash 
4. Exit 
Please enter your choice: `);
  
  // 优化输入验证
  if (!isNaN(accountType) && accountType >=1 && accountType <=4) {
    switch (accountType) {
      case 1:
        balance();
        break;
      case 2:
        withdrawal();
        break;
      case 3:
        deposit();
        break;
      case 4:
        exit();
        break;
      default:
        console.log("Please enter a valid option (1-4).");
        selectAccountType();
    }
  } else {
    console.log("Invalid input, please enter a number between 1 and 4.");
    selectAccountType();
  }
}

// 查询余额
function balance() {
  console.log(`Your current balance is £${currentBalance}`); // 用console.log输出,而非questionInt
  toContinue();
}

// 存款功能
function deposit() {
  const depositAmount = cashMachine.questionInt("How much do you want to deposit? £");
  if (!isNaN(depositAmount) && depositAmount > 0) { // 确保是正数
    currentBalance += depositAmount;
    console.log(`You have successfully deposited £${depositAmount}.\nYou now have £${currentBalance}.`);
    toContinue();
  } else {
    console.log("Error: Please enter a valid positive number!");
    deposit();
  }
}

// 取款功能
function withdrawal() {
  const withdrawalAmount = cashMachine.questionInt(`How much do you want to withdraw? 
The minimum amount you can withdraw is £5: £`);
  
  if (!isNaN(withdrawalAmount)) {
    if (withdrawalAmount >= 5) {
      if (withdrawalAmount <= currentBalance) {
        currentBalance -= withdrawalAmount;
        console.log("Transaction successful!");
        console.log(`Your remaining balance is £${currentBalance}.`);
        toContinue();
      } else {
        console.log("You do not have sufficient funds!");
        withdrawal();
      }
    } else {
      console.log("You must withdraw at least £5.");
      withdrawal();
    }
  } else {
    console.log("Error: Please enter a number!");
    withdrawal();
  }
}

// 选择是否继续操作
function toContinue() {
  const yesOrNo = cashMachine.questionInt(`Do you want to perform another transaction? 
1. Yes 
2. No 
Please enter your choice: `);
  
  if (!isNaN(yesOrNo)) {
    if (yesOrNo === 2) {
      exit();
    } else if (yesOrNo ===1) {
      selectAccountType();
    } else {
      console.log("Please enter 1 for Yes or 2 for No.");
      toContinue();
    }
  } else {
    console.log("Invalid input, please enter 1 or 2.");
    toContinue();
  }
}

// 退出程序
function exit() {
  console.log("Thank you for using our cash machine.");
  process.exit(); // 真正终止Node.js进程
}

// 启动程序!
cashMachineName();

修复说明

  1. 添加了程序启动入口cashMachineName(),确保程序运行时能进入初始流程
  2. 修正了所有questionInt的调用方式,统一通过cashMachine.questionInt()调用
  3. 调整PIN验证正则为只匹配4位数字,符合实际输入场景
  4. 替换alert为console.log,适配Node.js环境
  5. 修复了所有拼写错误,优化了输入验证逻辑
  6. 调整密码重试逻辑,避免循环与递归混用的问题
  7. 在exit()中加入process.exit(),确保程序能正常终止
  8. 优化了用户提示信息,让交互更清晰

内容的提问来源于stack exchange,提问作者Ruby Pepper

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 13:56:00