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

Lua银行程序输入数字后无响应直接退出问题求助

Lua银行程序无响应/自行关闭问题修复

你的程序出现问题主要有几个核心原因,对应修复方案如下:

核心问题分析与修复

  1. 字符串与数字类型不匹配
    io.read()默认读取的是字符串类型,你直接用choose == 1这种数字比较,永远不会匹配成功,所有功能分支都不会执行,程序直接走到末尾结束,看起来就是输入后无响应然后自行关闭。
    修复:把输入转为数字,同时处理非数字输入的情况:
local choose = tonumber(io.read())
if not choose then
    print("Invalid input, please enter a number between 1-4")
    io.read() -- 暂停让用户看到提示
    return
end
  1. 取款逻辑错误
  • 原代码先扣钱再判断余额不足,顺序完全颠倒,会导致余额变成负数
  • 循环内部重新定义了local withdraw_money,外层变量不会被更新,会陷入死循环
  • 余额提示的位置错误,应该在成功扣款后输出

修复后的取款分支:

if choose == 1 then
    io.write("Enter the amount you want to withdraw: ")
    local withdraw_money = tonumber(io.read())
    -- 先验证输入和余额
    while not withdraw_money or withdraw_money <=0 or withdraw_money > money_on_card do
        if not withdraw_money or withdraw_money <=0 then
            io.write("Please enter a valid positive number: ")
        else
            io.write("You don't have enough money. Please enter a valid amount: ")
        end
        withdraw_money = tonumber(io.read())
    end
    -- 验证通过再扣钱
    money_on_card = money_on_card - withdraw_money
    print("Money on your card: ".. money_on_card)
end
  1. 程序执行后直接关闭
    在Windows控制台运行Lua脚本时,程序执行完毕会立刻关闭窗口,导致你看不到输出。修复:在程序末尾添加暂停语句:
io.write("Press any key to exit...")
io.read()

完整修复后的代码

money_on_card = 500
print("Hello! This is your bank, Kudo banking.")
io.write('Press 1 to withdraw, 2 to deposit, 3 to check your money, 4 to leave: ')

local choose = tonumber(io.read())
if not choose then
    print("Invalid input, please enter a number between 1-4")
    io.write("Press any key to exit...")
    io.read()
    return
end

if choose == 1 then
    io.write("Enter the amount you want to withdraw: ")
    local withdraw_money = tonumber(io.read())
    while not withdraw_money or withdraw_money <=0 or withdraw_money > money_on_card do
        if not withdraw_money or withdraw_money <=0 then
            io.write("Please enter a valid positive number: ")
        else
            io.write("You don't have enough money. Please enter a valid amount: ")
        end
        withdraw_money = tonumber(io.read())
    end
    money_on_card = money_on_card - withdraw_money
    print("Money on your card: ".. money_on_card)
elseif choose == 2 then
    io.write("Enter the amount you want to deposit: ")
    local deposit_money = tonumber(io.read())
    while not deposit_money or deposit_money <=0 do
        io.write("Please enter a valid positive number: ")
        deposit_money = tonumber(io.read())
    end
    money_on_card = money_on_card + deposit_money
    print("Money on your card: ".. money_on_card)
elseif choose == 3 then
    print("Money on your card: ".. money_on_card)
elseif choose == 4 then
    print("Thanks for choosing us!")
else
    print("Invalid option, please enter a number between 1-4")
end

io.write("Press any key to exit...")
io.read()

另外把多个独立的if改成elseif更高效,同时增加了输入合法性校验,避免非数字输入导致程序出错。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.17 08:35:19