0

これは奇妙な問題のように思えます。入力文字列を受け取り、 and を使用scanして特定の値を抽出するこのビットの Ruby コードがあります。ステートメントでflattenその値を操作することを望んでいますが、問題があります。if...then

私の入力文字列は、その地域の「危険生物」の数を表しています。危険なクリーチャーが存在しない、または 2 体以上のクリーチャーを表す文字列は、常に次のような標準的なものです。

「そのエリアに危険な生き物はいない」または「危険な生き物が 1 匹...」など。

これを使用して、クリーチャーの数を表す単語を「いいえ」、「1 つ」、「2 つ」などとしてフェッチし、後でこれらを数値に変換することを期待しています - 「いいえ」= 0、「1 つ」 " = 1 など

これを行うには、次を使用します。

crittercount = strcheck.scan(/a*(no|one|two|three|four|five|six|seven|eight|nine|ten)a*/)

次にif then、変数crittercountに対して次のように実行します。

if crittercount == "no"
  critters = 0
  ... exit and go do something with the var...
end

これをそれぞれにしています。(この問題を理解したら、使用しますif..elsif..end)

if crittercount == "one"
  critters = 1
  ... exit and go do something with the var...
end
...

crittercount == "ten"私はちょうど使用した後

if crittercount == "ten"
  critters = 10
else
  critters = 99
end

maxcreaturesここに問題があります: equal toという変数があります10strcheckたとえば、「その地域に危険な生き物はいない」という値を取得し、正確な文字列を返す値を出力します。次に、crittercount 変数を出力します。この例では、「いいえ」と表示されます。

ただし、ステートメントを理解したらif..then..、次を使用して何をすべきかを評価します。

if critters > maxcreatures
  print "Maximum Creatures " + critters.to_s + " and maximum is #{maxcreatures}.  Lets Bail"
else
  print "Critter count " + critters.to_s  + " is less than #{maxcritters} Keep going."
end

私が得ているあらゆる状況で99、私はすべてを試したことを誓います。.flatten正規表現の最後で使ってみたり、var で使って.stripみたりcrittercount。誰かがこれを見て、「まあ、これを試してみてください」と言ってくれることを願っています。

リクエストに応じて、ここにすべてのコードがあります。ここには、意味をなさない可能性のある他の関数への呼び出しがあります...

maxcritters = 2

critters = 0


put "count critter"
strcheck = matchfind "You notice ?"

crittercount = strcheck.scan(/a*(no|one|two|three|four|five|six|seven|eight|nine|ten)a*/)
echo strcheck
echo crittercount
crittercount = crittercount.strip
if crittercount == "no"
    critters = 0
    goto "roundup"
end
if crittercount  == "one"
    critters = 1
    goto "roundup"
end
if crittercount == "two"
    critters = 2
    goto "roundup"
end
if crittercount == "three"
    critters = 3
    goto "roundup"
end
if crittercount == "four"
    critters = 4
    goto "roundup"
end
if crittercount == "five"
    critters = 5
    goto "roundup"
end
if crittercount == "six"
    critters = 6
    goto "roundup"
end
if crittercount == "seven"
    critters = 7
else
critters = 99
end

roundup:
if critters > maxcritters
echo "Maximum Creatures " + critters.to_s + " and maximum is #{maxcritters}.  Lets Bail"
critters == nil
fput "retreat"

else
    echo "Critter count " + critters.to_s  + " is below maximum of #{maxcritters} - We're cool.  Keep going."
    critters == nil
    goto "combatcheck"
end
4

4 に答える 4

1

scan複数の一致が予想される場合に便利です。文字列ごとに 1 つの一致しか期待できないため、それを使用しないでください。

以下は直接あなたに与えるでしょうcritters。これらすべての条件は必要ありません。

regex = /(\bno\b)|(\bone\b)|(\btwo\b)|(\bthree\b)|(\bfour\b)|(\bfive\b)
            |(\bsix\b)|(\bseven\b)|(\beight\b)|(\bnine\b)|(\bten\b)/x

critters = strcheck.match(regex).to_a.drop(1).index{|x| x} || 99
于 2012-10-24T12:33:09.523 に答える
0

問題はcrittercount、文字列ではなく配列であることです。したがって、if crittercount == "no"などは決してなくtrue、常に最後になりますelse

解決策は、コードを次のように変更することです。

crittercount = strcheck.scan(/no|one|two|three|four|five|six|seven|eight|nine|ten/).first

これcrittercountを使用すると、文字列またはnilコードの残りの部分が期待どおりに機能します。

于 2012-10-24T12:19:34.213 に答える
0

scan は、一致データの配列を含む配列を返すため、コードで .first.first を呼び出すと、最初の一致が得られます。このようなこともできます

strcheck = "two dangerous creatures in the area" # or "one dangerous creature...'
strcheck =~ (/(.*) dangerous.*/)
critters = case $1 
  when 'no' then 0
  when 'one' then 1
  when 'two' then 2
  when 'three' then 3
  when 'four' then 4
  when 'five' then 5
  when 'six' then 6
  when 'seven' then 7
  else 99 
end
puts "number of critters #{critters}"

ケースを使用するとロジックがクリーンアップされ、正規表現を使用して最初の一致を照合して抽出するだけです

于 2012-10-24T12:36:57.853 に答える
0

if...end if...end if...end ではなく if...elsif..elsif...end を実行しているようです

したがって、10 以外の値の場合は、この else ブロックになります。

if crittercount == "ten"
  critters = 10
else
  critters = 99
end

ここではケースステートメントの方が良いでしょう...

case (crittercount)
    when "no" then critters = 0
    when "one" then critters = 1
    when "two" then critters = 2
    when "three" then critters = 3
    when "four" then critters = 4
    when "five" then critters = 5
    when "six" then critters = 6
    when "seven" then critters = 7
    when "eight" then critters = 8
    when "nine" then critters = 9
    when "ten" then critters = 10
    else critters = 99
end

またはさらに良いことに、ルックアップが失敗した場合にデフォルトで 99 になるすべての数値のハッシュ ルックアップ。

numberHash = { "no" => 0, "one" => 1, "two" => 2, "three" => 3, "four" => 4, "five" => 5, "six" => 6, "seven" => 7, "eight" => 8, "nine" => 9, "ten" => 10 }
numberHash.default = 99
critters = numberHash[critterCount]
于 2012-10-24T12:22:40.363 に答える