Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基本Lua循环不能正常工作_Lua - Fatal编程技术网

基本Lua循环不能正常工作

基本Lua循环不能正常工作,lua,Lua,基本上我有一个计算器,你可以在除法,乘法,加减之间进行选择。我刚刚学会了如何循环程序。但循环并没有像它应该的那样工作 print("Please choose the way to use the calculator") print("[1] Plus [2] Minus [3] Division [4] Multiply") restart = 1 x = tonumber(io.read()) while restart == 1 do if x == 1 then pri

基本上我有一个计算器,你可以在除法,乘法,加减之间进行选择。我刚刚学会了如何循环程序。但循环并没有像它应该的那样工作

print("Please choose the way to use the calculator")
print("[1] Plus [2] Minus [3] Division [4] Multiply")

restart = 1

x = tonumber(io.read())

while restart == 1 do

if x == 1 then
    print("Please write the first number to add up")
    n1 = tonumber(io.read())
    print("Please write the second number to add up")
    n2 = tonumber(io.read())
    print(n1 .. "+" .. n2 .. "=" .. n1+n2)
elseif x == 2 then
    print("Please write the first number to subtract")
    n1 = tonumber(io.read())
    print("Please write the second number to subtract")
    n2 = tonumber(io.read())
    print(n1 .. "-" .. n2 .. "=" .. n1-n2)
elseif x == 3 then
    restart = 0
    print("Please write the first number to divide")
    n1 = tonumber(io.read())
    print("Please write the second number to divide")
    n2 = tonumber(io.read())
    print(n1 .. "/" .. n2 .. "=" .. n1/n2)
elseif x == 4 then
    print("Please write the first number to multiply")
    n1 = tonumber(io.read())
    print("Please write the second number to multiply")
    n2 = tonumber(io.read())
    print(n1 .. "*" .. n2 .. "=" .. n1*n2)
end
end
所以基本上,如果你选择负数,然后加上10-2。这是它应该做的。但问题是只有负的部分继续循环。它并不要求你选择一种乘法的方法。我怎样才能解决这个问题


例如,我想让它为你做一个等式,然后循环回到开头。

你不需要把输入放在循环中吗

x = tonumber(io.read())

while restart == 1 do
宁可

while restart == 1 do

x = tonumber(io.read())

你不需要把输入放在你的循环中吗

x = tonumber(io.read())

while restart == 1 do
宁可

while restart == 1 do

x = tonumber(io.read())

你不需要把输入放在你的循环中吗

x = tonumber(io.read())

while restart == 1 do
宁可

while restart == 1 do

x = tonumber(io.read())

你不需要把输入放在你的循环中吗

x = tonumber(io.read())

while restart == 1 do
宁可

while restart == 1 do

x = tonumber(io.read())

您可以将其替换为:

print("Please choose the way to use the calculator")
print("[1] Plus [2] Minus [3] Division [4] Multiply")

restart = 1

x = tonumber(io.read())

while restart == 1 do
据此:

restart = 1

while restart == 1 do

print("Please choose the way to use the calculator")
print("[1] Plus [2] Minus [3] Division [4] Multiply")

x = tonumber(io.read())
另一方面,由于您正在学习Lua,下面是一种重构代码的方法(通过正确使用局部变量等):


您可以将其替换为:

print("Please choose the way to use the calculator")
print("[1] Plus [2] Minus [3] Division [4] Multiply")

restart = 1

x = tonumber(io.read())

while restart == 1 do
据此:

restart = 1

while restart == 1 do

print("Please choose the way to use the calculator")
print("[1] Plus [2] Minus [3] Division [4] Multiply")

x = tonumber(io.read())
另一方面,由于您正在学习Lua,下面是一种重构代码的方法(通过正确使用局部变量等):


您可以将其替换为:

print("Please choose the way to use the calculator")
print("[1] Plus [2] Minus [3] Division [4] Multiply")

restart = 1

x = tonumber(io.read())

while restart == 1 do
据此:

restart = 1

while restart == 1 do

print("Please choose the way to use the calculator")
print("[1] Plus [2] Minus [3] Division [4] Multiply")

x = tonumber(io.read())
另一方面,由于您正在学习Lua,下面是一种重构代码的方法(通过正确使用局部变量等):


您可以将其替换为:

print("Please choose the way to use the calculator")
print("[1] Plus [2] Minus [3] Division [4] Multiply")

restart = 1

x = tonumber(io.read())

while restart == 1 do
据此:

restart = 1

while restart == 1 do

print("Please choose the way to use the calculator")
print("[1] Plus [2] Minus [3] Division [4] Multiply")

x = tonumber(io.read())
另一方面,由于您正在学习Lua,下面是一种重构代码的方法(通过正确使用局部变量等):



将x设置在循环之外,这样它就不会改变。将您的x=输入放入循环中,这样每次迭代都会重新读取它

将x设置在循环之外,这样它就不会改变。将您的x=输入放入循环中,这样每次迭代都会重新读取它

将x设置在循环之外,这样它就不会改变。将您的x=输入放入循环中,这样每次迭代都会重新读取它

将x设置在循环之外,这样它就不会改变。将您的x=输入放入循环中,这样每次迭代都会重新读取它

您只在代码的除法部分将
restart
值更改为0。@Kamiccolo是的,我看到了。这并不是问题的原因。您只是在代码的除法部分将
restart
值更改为0。@Kamiccolo是的,我看到了。这并不是问题的原因。您只是在代码的除法部分将
restart
值更改为0。@Kamiccolo是的,我看到了。这并不是问题的原因。您只是在代码的除法部分将
restart
值更改为0。@Kamiccolo是的,我看到了。这并不是问题的原因,是我做的,现在在得到方程的答案后,什么也没有发生。我可以一直按enter键,它只打印我键入的内容。如果我一直按enter键,就会有空格。我这样做了,现在在得到方程的答案后,什么也不会发生。我可以一直按enter键,它只打印我键入的内容。如果我一直按enter键,就会有空格。我这样做了,现在在得到方程的答案后,什么也不会发生。我可以一直按enter键,它只打印我键入的内容。如果我一直按enter键,就会有空格。我这样做了,现在在得到方程的答案后,什么也不会发生。我可以一直按enter键,它只打印我键入的内容。如果我一直按enter键,就会出现空格。IIRC可以简单地使用io.read(“*n”)而不是get_num()函数。IIRC可以简单地使用io.read(“*n”)而不是get_num()函数。IIRC可以简单地使用io.read(“*n”)而不是get_num()函数。IIRC可以简单地使用io.read(“*n”)而不是get_num()函数。