如何更改 lua 中的原始变量
How can I change the original variable in lua
我想更改原始变量,以便函数打印不同的答案。我是 Lua 的新手,这可能吗?我该怎么做?
这是我的代码:
io.write("Hello, what is your first player's name? ")
local name = io.read()
io.write('How old is Player 1? ')
local age = io.read()
function Recall()
print("Your player's name: " ,name, "\nYour player's age: " ,age, "\n")
end
Recall()
io.write("What is your second player's name? ")
local name = io.read()
io.write('How old is Player 2? ')
local age = io.read()
Recall()
当我第二次调用该函数时,它会在第一次输入时打印姓名和年龄。有什么建议吗?
您创建了新的本地人并失去了对同名本地人的访问权限。
要解决此问题,请移除第二个 name
和第二个 age
附近的 local
。 (在 Player2
附近)。
作为替代解决方案,您可以为 Recall
函数设置参数并在其中传递参数。
好主意,稍微调整一下,这样您就不会使用相同的变量
io.write("Hello, what is your first player's name? ")
local name1 = io.read()
io.write('How old is Player 1? ')
local age1 = io.read()
function Recall( name, age )
print("Your player's name: ", name, "\nYour player's age: ", age, "\n")
end
Recall( name1, age1 )
io.write("What is your second player's name? ")
local name2 = io.read()
io.write('How old is Player 2? ')
local age2 = io.read()
Recall( name2, age2 )
我想更改原始变量,以便函数打印不同的答案。我是 Lua 的新手,这可能吗?我该怎么做?
这是我的代码:
io.write("Hello, what is your first player's name? ")
local name = io.read()
io.write('How old is Player 1? ')
local age = io.read()
function Recall()
print("Your player's name: " ,name, "\nYour player's age: " ,age, "\n")
end
Recall()
io.write("What is your second player's name? ")
local name = io.read()
io.write('How old is Player 2? ')
local age = io.read()
Recall()
当我第二次调用该函数时,它会在第一次输入时打印姓名和年龄。有什么建议吗?
您创建了新的本地人并失去了对同名本地人的访问权限。
要解决此问题,请移除第二个 name
和第二个 age
附近的 local
。 (在 Player2
附近)。
作为替代解决方案,您可以为 Recall
函数设置参数并在其中传递参数。
好主意,稍微调整一下,这样您就不会使用相同的变量
io.write("Hello, what is your first player's name? ")
local name1 = io.read()
io.write('How old is Player 1? ')
local age1 = io.read()
function Recall( name, age )
print("Your player's name: ", name, "\nYour player's age: ", age, "\n")
end
Recall( name1, age1 )
io.write("What is your second player's name? ")
local name2 = io.read()
io.write('How old is Player 2? ')
local age2 = io.read()
Recall( name2, age2 )