英文:
How do i make a variable have random numbers that are strings every time the code is run
问题
我正在尝试在Lua中创建一个猜数字游戏,我已经完成了其他所有部分,现在唯一需要做的是在每次运行代码时生成一个名为"answer"的字符串变量中的随机数字。
这是我目前拥有的代码:answer = tostring(math.random(1, 100))
。我的问题是,我需要它在每次运行代码时生成一个1-100之间的随机数字,并将它放入变量"answer"中。
英文:
I am attempting to make a guessing game in Lua i have did everything else the only thing I need to do now is make it generate random numbers that are strings in a variable called answer every time the code is run.
this is the code I have right now answer = math.randomseed(1, 100)(os.time())
the problem i have is that i need it to come up with a number that is a string, 1-100, random every time the code is run and for it to be put into the variable answer
答案1
得分: 1
本地答案 = 转字符串(取随机数(1,100))
打印(本地答案)
英文:
local ans = tostring(math.random(1,100))
print(ans)
答案2
得分: 1
Use string.format()
函数将你想要的内容格式化为字符串。
local seed = math.randomseed(math.random(os.time()))
local fmt = ('%d'):format(math.random(1, 100))
print(type(fmt), fmt:rep(10, ' > '))
-- 示例输出:string 99 > 99 > 99 > 99 > 99 > 99 > 99 > 99 > 99 > 99
然后创建一个函数。
local seed = math.randomseed(math.random(os.time()))
local function rn(mini, maxi)
local mini, maxi = mini or 1, maxi or 6 -- 没有参数默认为骰子
return(('%d'):format(math.random(mini, maxi)))
end
for i = 1, 10 do print(type(rn(1, 100)), rn(1, 100)) end
输出示例:
string 56
string 75
string 86
string 98
string 35
string 36
string 81
string 33
string 1
string 23
英文:
Use string.format()
to put what you want out as a String.
local seed = math.randomseed(math.random(os.time()))
local fmt = ('%d'):format(math.random(1, 100))
print(type(fmt), fmt:rep(10, ' > '))
-- Example output: string 99 > 99 > 99 > 99 > 99 > 99 > 99 > 99 > 99 > 99
Than make a Function
local seed = math.randomseed(math.random(os.time()))
local function rn(mini, maxi)
local mini, maxi = mini or 1, maxi or 6 -- No Arguments defaults to a Dice
return(('%d'):format(math.random(mini, maxi)))
end
for i = 1, 10 do print(type(rn(1, 100)), rn(1, 100)) end
Output Example
string 56
string 75
string 86
string 98
string 35
string 36
string 81
string 33
string 1
string 23
答案3
得分: 0
我做了一个随机数游戏,我的代码是 "math.randomseed(os.time())
local secret_number = math.random(1, 100)
print("猜猜数字是多少,1-100")
repeat
local guess_number = io.read("*n")
if not guess_number then print("无效输入!")
elseif guess_number < secret_number then print("太低了,再试一次!")
elseif guess_number > secret_number then print("太高了,再试一次!")
end
until guess_number == secret_number
print("你猜对了!恭喜"
英文:
I made a random number game and my code was "math.randomseed(os.time())
local secret_number = math.random(1, 100)
print("guess what the number is, 1-100")
repeat
local guess_number = io.read("*n")
if not guess_number then print("invalid input!")
elseif guess_number < secret_number then print("too low, try again!")
elseif guess_number > secret_number then print("too high, try again!")
end
until guess_number == secret_number
print("you got it!! congrats")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论