Рубин с отсутствующим концом.Или дополнительный конец? - PullRequest
0 голосов
/ 05 декабря 2018

Я видел этот ответ раньше, но я просто не могу найти недостающий конец здесь.Это простая Ruby-программа для обучения.Я вернулся и напечатал это точно так, как они это сделали.Все еще получаю ошибку.Где конец пропущен ????

puts "Welcome to 'Get My Number!'"
print "What's your name? "

input = gets

name = input.chomp

puts "Welcome, #{name}!"

# Store a random number for the player to guess.

puts "I've got a random number between 1 and 100."
puts "Can you guess it?"
target = rand(100) + 1

# Track how many guesses the player has made.

num_guesses = 0

# Track whether player has guessed correctly.

guessed_it = false

Until num_guesses == 10 || guessed_it

  puts "You've got #{10 - num_guesses} guesses left."
  print "Make a guess: "
  guess = gets.to_i

  num_guesses += 1

# Compare guess to target and print appropriate message.

  if guess < target
    puts "Oops. Your guess was LOW."
  elsif guess > target
    puts "Oops. Your guess was HIGH."
  elsif guess == target
    puts "Good job, #{name}!"
    puts "You guessed my number in #{num_guesses} guesses!"
    guessed_it = true
  end

 end

 #If the player didn't guess in time, show the target number.

 unless guessed_it
    puts "Sorry. You didn't get my number. (It was #{target}.)"

Ответы [ 2 ]

0 голосов
/ 05 декабря 2018

Я думаю, что проблема в if и elsif блоке.Измените его на case.Кроме того, он более понятен и прост для понимания.

Например, таким образом:

case 
when guess < target then puts "Oops. Your guess was LOW."
when guess > target then puts "Oops. Your guess was HIGH."
when guess == target then
   puts "Good job, #{name}!"
   puts "You guessed my number in #{num_guesses} guesses!"
   guessed_it = true
end

Надеюсь, это будет полезно для вас.

0 голосов
/ 05 декабря 2018

Как упоминал Том Лорд, в конце вы пропускаете end, а ключевое слово Until должно быть в нижнем регистре для Сагар Пандья.Ваша заявка в целом должна быть такой, как показано ниже.

puts "Welcome to 'Get My Number!'"
print "What's your name? "

input = gets

name = input.chomp

puts "Welcome, #{name}!"

# Store a random number for the player to guess.

puts "I've got a random number between 1 and 100."
puts "Can you guess it?"
target = rand(100) + 1

# Track how many guesses the player has made.

num_guesses = 0

# Track whether player has guessed correctly.

guessed_it = false

until num_guesses == 10 || guessed_it

  puts "You've got #{10 - num_guesses} guesses left."
  print "Make a guess: "
  guess = gets.to_i

  num_guesses += 1

# Compare guess to target and print appropriate message.

  if guess < target
    puts "Oops. Your guess was LOW."
  elsif guess > target
    puts "Oops. Your guess was HIGH."
  elsif guess == target
    puts "Good job, #{name}!"
    puts "You guessed my number in #{num_guesses} guesses!"
    guessed_it = true
  end
end

#If the player didn't guess in time, show the target number.

unless guessed_it
  puts "Sorry. You didn't get my number. (It was #{target}.)"
end
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...