首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >怎样才能让if语句从math.random的多个数字中说出来?

怎样才能让if语句从math.random的多个数字中说出来?
EN

Stack Overflow用户
提问于 2022-01-14 19:52:16
回答 2查看 146关注 0票数 -1
代码语言:javascript
复制
local x = math.random(1,500)

if x == 1 then 
  print("You Got a g")
end
if x == 2-10 then
  print("You Got a f")
end
if x == "11,100" then
  print("You Got a d")
end
if x == "101,200" then
  print("You Got a c")
end
if x == 201-300 then
  print("You Got a b")
end
if x == 301-500 then
  print("You Got a")
end

print(x)

所以在这个脚本中,我不知道当我把(if x == (what to put here) then)放在

我还在学剧本,所以没那么好

EN

回答 2

Stack Overflow用户

发布于 2022-01-15 10:34:06

除了Reinisdm答案之外,您还可以简化条件,因为第一个表达式总是真的。

代码语言:javascript
复制
local x = math.random(1,500)

if x == 1 then 
    print("You Got a g") 
elseif x <= 10 then 
    print("You Got a f") 
elseif x <= 100 then 
    print("You Got a d") 
elseif x <= 200 then 
    print("You Got a c") 
elseif x <= 300 then 
    print("You Got a b") 
elseif x <= 500 then 
    print("You Got a") 
end
print(x)
票数 2
EN

Stack Overflow用户

发布于 2022-01-14 20:16:35

要检查多个数字,请使用<<=>>=and

代码语言:javascript
复制
local x = math.random(1,500)

if x == 1 then 
    print("You Got a g") 
elseif 2 <= x and x <= 10 then 
    print("You Got a f") 
elseif 11 <= x and x <= 100 then 
    print("You Got a d") 
elseif 101 <= x and x <= 200 then 
    print("You Got a c") 
elseif 201 <= x and x <= 300 then 
    print("You Got a b") 
elseif 301 <= x and x <= 500 then 
    print("You Got a") 
end 
print(x)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70715866

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档