【问题标题】:Ruby Guessing Game w 'Loop Do'Ruby 猜谜游戏带“Loop Do”
【发布时间】:2017-06-23 16:45:22
【问题描述】:

我通过 Ruby 创建了一个猜谜游戏,我相信我的代码结构是错误的。输入“作弊”时,您会得到一个随机数,然后要求您再次输入。再次输入时,它说随机数不正确,在第 45 行始终默认为我的“elseif”。

puts "Hey! I'm Sam. What's your name?"
name = gets
puts "Welcome #{name}. Thanks for playing the guessing game.
I've chosen a number between 1-100.
You'll have 10 tries to guess the correct number.
You'll also recieve a hint when you're guess is wrong.
If you feel like being a big ol cheater, type 'Cheat'.
Let's get started..."

random_number = rand(1...100)
Cheat = random_number
counter = 10

loop do
 break if counter == 0
 divisor = rand(2...10)
 guess = gets.chomp
  break if guess.to_i == random_number
 counter -= 1
 if
   guess == random_number
   puts 'You guessed the right number! You win!'
 end
 if counter < 4
   puts "You can go ahead and cheat by typing 'Cheat'..."
 end
  if guess.to_s.downcase.eql? "cheat"
    puts "The random number is #{random_number} you CHEATER!! Go ahead and type it in..."
    guess = gets.chomp
    puts = "You win cheater!"
  end
 if
     guess.to_i < random_number
     puts 'Ah shucks, guess again!'
     guess = gets.chomp
 elsif
     guess.to_i > random_number
     puts 'Too high, guess again!'
     guess = gets.chomp
 end

 if random_number % divisor == 0
   puts "Thats not it.\n #{guess} is #{guess.to_i > random_number ? 'less' : 'greater'} than the random number.
   The random number is divisible by #{divisor}.\nTry again: "
 elsif
   puts "That's not the random number.\n #{guess} is #{guess.to_i > random_number ? 'less' : 'greater'} than the random number.
   The random number is NOT divisible by #{divisor}.\nTry again: "
 end
end

if counter > 0
  puts "The number is #{random_number}! You win!"
else
  puts "You lose! Better luck another time."
end

这是我在终端得到的响应

Let's get started...
Cheat
The random number is 96 you CHEATER!! Go ahead and type it in...
96
Thats not it.
 96 is greater than the random number.
   The random number is divisible by 8.
Try again: 

【问题讨论】:

    标签: ruby terminal atom-editor


    【解决方案1】:

    问题出在这里:

    puts = "You win cheater!"
    

    您将字符串 "You win cheater!" 分配给名为 puts 的局部变量。将其更改为此可以解决问题:

    puts "You win cheater!"
    

    您可能还想在该行后面加上break


    顺便说一句,这个模式:

    loop do
      break if counter == 0
      # ...
    end
    

    ...最好表达为:

    while counter > 0
      # ...
    end
    

    ...或:

    until counter == 0
      # ...
    end
    

    此外,您应该始终if/elsif/whathaveyou 的条件与if 等放在同一行。为什么?因为如果你不这样做,你会得到这样的错误:

    if random_number % divisor == 0
      # ...
    elsif
      puts "..."
    end
    

    你能发现错误吗?你忘了在elsif后面加上一个条件,或者当你打算使用else时使用了elsif,这意味着puts的返回值(总是nil)被用作条件,就像你写了elsif puts "..."一样。

    如果您养成始终将条件与if/elsif 放在同一行的习惯,您的眼睛就会习惯它,这样的错误就会跳出来。

    【讨论】:

    • 谢谢!多亏了你,我注意到了一些错误。感谢您的帮助!
    猜你喜欢
    • 2015-10-22
    • 1970-01-01
    • 1970-01-01
    • 2022-09-26
    • 2015-04-15
    • 2020-11-22
    • 2014-05-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多