【问题标题】:How can I execute a function when key is pressed in Lua?在Lua中按下键时如何执行功能?
【发布时间】:2020-07-23 15:02:51
【问题描述】:

我只想在按下“+”键时执行 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

【问题讨论】:

  • 我认为您的问题可以更简洁。改变这个: ** 只想在按下“+”键时执行 findSum() 函数。所以当我按“+”键时,必须调用函数 findSum()。到目前为止,这是我的程序:** 到此:如何知道何时按下“+”键 并删除/使您的代码更短。还请务必在互联网上搜索我更正的问题,因为它似乎是一个无处不在的话题。还为您尝试完成任务的环境提供上下文。查看:stackoverflow.com/questions/5689566/keypress-event-in-lua
  • 这能回答你的问题吗? KeyPress event in Lua?
  • 感谢您的帮助。我非常感谢。我刚刚在另一个程序中测试了同样的问题并得到了解决。

标签: function keypress execution


【解决方案1】:

我运行了您的代码并收到了此输出。你期待什么?

        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

请注意,我在运行代码之前修复了缩进。

【讨论】:

    【解决方案2】:

    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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-04
      • 1970-01-01
      • 2020-07-07
      • 1970-01-01
      • 2022-09-23
      • 2020-05-02
      • 1970-01-01
      相关资源
      最近更新 更多