2

これは私がこれまでに持っているものですが、実行しようとするたびに閉じているようです。

function wait(seconds)
  local start = os.time()
  repeat until os.time() > start + seconds
  end

function random(chance)
  if math.random() <= chance then
  print ("yes")
  elseif math.random() > chance then
  print ("no")
  end

random(0.5)
wait(5)
end

それが完全なコンテキストです。

4

2 に答える 2

8

そのコードにはいくつかの問題があります。最初 (Lorenzo Donati が指摘しているように) は、実際の呼び出しを の中にラップして、実際random()には何もしないチャンクを与えていることです (おっと)。

2 つ目は、2 回呼び出すことmath.random()で、2 つの個別の値が得られることです。これは、どちらのテストも真ではない可能性が完全にあることを意味します。

3 番目はマイナーです。どちらかまたはどちらかを選択するために 2 つのテストを行っています。最初のテストは、私たちが知る必要があるすべてを教えてくれます:

function wait(seconds)
    local start = os.time()
    repeat until os.time() > start + seconds
end

function random(chance)
    local r = math.random()
    if r <= chance then
        print ("yes")
    else
        print ("no")
    end
end

random(0.5)
wait(5)

キックのために、if ブロックを条件付き値に置き換えることができます。

function wait(seconds)
    local start = os.time()
    repeat until os.time() > start + seconds
end

function random(chance)
    local r = math.random()
    print(r<=chance and "yes" or "no")
end

random(0.5)
wait(5)
于 2013-08-24T03:17:17.637 に答える
3

おそらくあなたはこれを書くつもりでした:

function wait(seconds)
  local start = os.time()
  repeat until os.time() > start + seconds
end

function random(chance)
    if math.random() <= chance then
        print ("yes")
    elseif math.random() > chance then
        print ("no")
    end
end

random(0.5)
wait(5)
于 2013-08-23T22:34:08.187 に答える