你好,我正在尝试创建一个简单的Ruby测试

plicqrtu  于 2023-03-22  发布在  Ruby
关注(0)|答案(1)|浏览(118)

我是新的编码,这是第一个适当的程序,我已经尝试编码。
第一轮的工作原理,因为我打算它,但第2和第3轮跳到线“把”错了!“把”你已经达到了最大数量的猜测。你得到零分,为这一轮“”
即使我正确地回答了问题。有什么建议可以解决这个问题吗?

points = 0
puts "Welcome to my game. There are three rounds of questions in this quiz."
puts "You get a point for each question you get right within 3 guesses" 
puts "Please press enter to continue"
gets

puts "Round 1"
puts "I am thinking of a random number between 1 and 100. You have 3 attempts to guess my number correctly"

random_number = rand(1 .. 100)

guess = ""
guess_count = 0
out_of_guesses = false

while guess != random_number and !out_of_guesses
    if guess_count == 0
        puts "Enter your guess:"
        guess = gets.chomp.to_i
        guess_count += 1
      
    elsif guess_count == 1
      if random_number > 50
        puts "Wrong! Try again"
        puts "Hint 1:"
        puts "My number is bigger than 50"
      elsif random_number < 50
        puts "Wrong! Try again"
        puts "Hint 1:"
        puts "My number is smaller than 50"
      end
        puts "Enter your second guess:"
        guess = gets.chomp.to_i
        guess_count += 1
      
    elsif guess_count == 2
      if guess > random_number 
        diff1 = guess - random_number
        puts "Wrong! Try again"
        puts "Hint 2:"
        puts "My number is #{diff1} numbers away from your guess"
      elsif guess < random_number
        diff2 = random_number - guess
        puts "Wrong! Try again"
        puts "Hint 2:"
        puts "My number is #{diff2} numbers away from your guess"
      end
        puts "Enter your final guess:"
        guess = gets.chomp.to_i
        guess_count += 1

    else 
        out_of_guesses = true
    end
end

if out_of_guesses
    puts "Wrong!"
    puts "You have reached the maximum number of guesses. You get zero points for this round."
    puts "The number I was thinking about was #{random_number}"
else
    points += 1
    puts "Congratulations! You get a point!"
    puts "You correctly guessed that the number I was thinking about is #{random_number} and you did it in #{guess_count} guesses."
end

puts "Press enter to continue"
gets

def answer_my_question(question, right_answer)
answer = ""
out_of_guesses = false
guess_count = 0
guess_limit = 3

puts "You get 3 tries to answer this question and there are no hints in this section"
puts question

while answer != right_answer and !out_of_guesses
  if guess_count == 0
    puts "Enter your first guess"
    answer = gets.chomp.downcase
    guess_count += 1
  elsif guess_count < guess_limit
    puts "Wrong! Try again"
    answer = gets.chomp
    guess_count += 1
  else
    out_of_guesses = true
end
end
end

puts "Round 2"
answer_my_question("What is the capital of Spain?", "madrid")

if out_of_guesses
    puts "Wrong!"
    puts "You have reached the maximum number of guesses. You get zero points for this round"
else
    points += 1
    puts "Congratulations! You get a point!"
    puts "You correctly answered the question."
end

puts "Press enter to continue"
gets

puts "Round 3"
answer_my_question("What continent is jamaica in?", "north america")

if out_of_guesses
    puts "Wrong!"
    puts "You have reached the maximum number of guesses. You get zero points for this round"
else
    points += 1
    puts "Congratulations! You get a point!"
    puts "You correctly answered the question."
end

puts "Press enter to continue"
gets

puts "This is the end of the quiz. You ended up with #{points} points"

我使用了downcase方法来尝试减轻任何大小写敏感性问题,但它不起作用

rdrgkggo

rdrgkggo1#

您遇到的问题是由于在answer_my_question函数外部声明了out_of_guesses变量。这会导致它在函数调用之间保留其值,这意味着如果在一次函数调用中将其设置为true,则在下一次函数调用中仍将保持为true,从而导致您遇到的问题。要解决此问题,请执行以下操作:你应该在answer_my_question函数中声明out_of_guesses变量,并将其作为函数的结果返回。

相关问题