在 Lua 中按下按键时如何执行功能?
How can I execute a function when key is pressed in Lua?
我只想在按下“+”键时执行 findSum() 函数。所以当我按“+”键时,然后
必须调用函数 findSum()。
这是我目前的程序:
numberOfInput = 0
sum = 0
io.write('\t', "Welcome to My Calculator", '\n')
repeat
io.write("for addition, press '+' :", '\n')
findSumSection = io.read()
until
(string.match(findSumSection, '+'))
function findSum()
io.write("Enter the amount of feed(s):",'\n')
numberOfInput = io.read()
x = numberOfInput
for i = 1, x, 1 do
io.write('\n',"enter the number: ")
inputs = io.read()
sum = sum + inputs
end
return sum
end
io.write('\n',"The Sum of your feeds is:", '\n' , findSum(), '\n')
io.write("Do you want to continue? [Y/N]")
inputYesorNo = io.read()
if (string.match(inputYesorNo, 'yes')) then
print("Starting the program again")
else if(string.match(inputYesorNo, 'no')) then
os.exit()
else
print("The program can not understand the procedure")
end
end
我 运行 你的代码并收到了这个输出。你在期待什么?
Welcome to My Calculator
for addition, press '+' :
+
Enter the amount of feed(s):
3
enter the number: 1
enter the number: 2
enter the number: 3
The Sum of your feeds is:
6
Do you want to continue? [Y/N]no
请注意,我修复了 运行 代码前的缩进。
Mike67 感谢您测试程序。我想我解决了这个问题。我想做的是,当我按下“+”键时,我想转到函数 findSum() 以使其执行。我在另一个程序中解决了这个问题。看看
function printMyName()
name = io.read()
return name
end
function findYourAge()
io.write("enter the current year:")
currentYear = io.read()
io.write('\n',"enter the year you were born:")
bornYear = io.read()
age = currentYear - bornYear
updatedAge = math.abs(age)
return updatedAge
end
function ageLimitToVote()
ageLimit = 18
newAgeForVote = 0
if(updatedAge < ageLimit) then
repeat
updatedAge = updatedAge + 1
until
updatedAge == ageLimit
newAgeForVote = updatedAge
return newAgeForVote
end
end
io.write("What is your name ", '\n')
io.write("your name is ", printMyName(), '\n')
io.write("Do you want to find your age?", '\n')
answer = io.read()
if(string.match(answer, 'yes')) then
findYourAge()
elseif(string.match(answer, 'no')) then
os.exit()
end
if(updatedAge > 16) then
io.write(name, " you are matured enough to vote", '\n')
else
io.write(name, " you are ", updatedAge, " years old. and you have to wait until you are ", ageLimitToVote(), '\n')
end
*这是我认为目前为止最好的解决方案。
if(string.match(answer, 'yes')) then
findYourAge()
我只想在按下“+”键时执行 findSum() 函数。所以当我按“+”键时,然后 必须调用函数 findSum()。 这是我目前的程序:
numberOfInput = 0
sum = 0
io.write('\t', "Welcome to My Calculator", '\n')
repeat
io.write("for addition, press '+' :", '\n')
findSumSection = io.read()
until
(string.match(findSumSection, '+'))
function findSum()
io.write("Enter the amount of feed(s):",'\n')
numberOfInput = io.read()
x = numberOfInput
for i = 1, x, 1 do
io.write('\n',"enter the number: ")
inputs = io.read()
sum = sum + inputs
end
return sum
end
io.write('\n',"The Sum of your feeds is:", '\n' , findSum(), '\n')
io.write("Do you want to continue? [Y/N]")
inputYesorNo = io.read()
if (string.match(inputYesorNo, 'yes')) then
print("Starting the program again")
else if(string.match(inputYesorNo, 'no')) then
os.exit()
else
print("The program can not understand the procedure")
end
end
我 运行 你的代码并收到了这个输出。你在期待什么?
Welcome to My Calculator
for addition, press '+' :
+
Enter the amount of feed(s):
3
enter the number: 1
enter the number: 2
enter the number: 3
The Sum of your feeds is:
6
Do you want to continue? [Y/N]no
请注意,我修复了 运行 代码前的缩进。
Mike67 感谢您测试程序。我想我解决了这个问题。我想做的是,当我按下“+”键时,我想转到函数 findSum() 以使其执行。我在另一个程序中解决了这个问题。看看
function printMyName()
name = io.read()
return name
end
function findYourAge()
io.write("enter the current year:")
currentYear = io.read()
io.write('\n',"enter the year you were born:")
bornYear = io.read()
age = currentYear - bornYear
updatedAge = math.abs(age)
return updatedAge
end
function ageLimitToVote()
ageLimit = 18
newAgeForVote = 0
if(updatedAge < ageLimit) then
repeat
updatedAge = updatedAge + 1
until
updatedAge == ageLimit
newAgeForVote = updatedAge
return newAgeForVote
end
end
io.write("What is your name ", '\n')
io.write("your name is ", printMyName(), '\n')
io.write("Do you want to find your age?", '\n')
answer = io.read()
if(string.match(answer, 'yes')) then
findYourAge()
elseif(string.match(answer, 'no')) then
os.exit()
end
if(updatedAge > 16) then
io.write(name, " you are matured enough to vote", '\n')
else
io.write(name, " you are ", updatedAge, " years old. and you have to wait until you are ", ageLimitToVote(), '\n')
end
*这是我认为目前为止最好的解决方案。
if(string.match(answer, 'yes')) then
findYourAge()