0

当我运行游戏并在最后输入“yes”或“no”时,它总是返回到第while41 行的循环开头,此时该循环和包含循环的条件均不满足。

replay = true
while replay

  #Pre-var
  print "What difficulty? (1 for easy, 2 for medium, or 3 for hard): "
  difficulty = gets.chomp.to_i
  until difficulty == 1 || difficulty == 2 || difficulty == 3 do
    print "Please type either 1, 2, or 3: "
    difficulty = gets.chomp.to_i  
  end

  #Variables
  if difficulty == 1
    r = Random.new
    number = r.rand(100..1000)
    puts "You have 15 guesses. "
    print "Guess a number with three digits: "
    within_num = 50
  elsif difficulty == 2
    r = Random.new
    number = r.rand(1000..10000)
    puts "You have 15 guesses. "
    print "Guess a number with four digits: "
    within_num = 500
  elsif difficulty == 3
    r = Random.new
    number = r.rand(10000..100000)
    puts "You have 15 guesses. "
    print "Guess a number with five digits: "
    within_num = 5000
  end
  guess = ""
  num_guess = 0
  guess_array = Array.new
  array_location = 0
  count_through = 0
  array_print = count_through - 1
  replay_inner = true

  #Body
  puts number
  while num_guess <= 14 || replay_inner == true #Keeping as <= 14 as to avoid unnecessarily rewriting code, still gives 15 guesses
    guess = gets.chomp.to_i
    if guess > number * 2
      print "That is more than double the number. Guess again: "
    elsif guess < number / 2
      print "That is less than half the number. Guess again: "
    elsif guess > number && guess < number + within_num #within_num: 50 for easy, 500 for medium, 5000 for hard
      print "You are close. That is too big. Guess again: "
    elsif guess < number && guess > number - within_num
      print "You are close. That is too small. Guess again: "
    elsif guess < number
      print "That is too small. Guess again: "                #Hinting the user to how close they are.
    elsif guess > number
      print "That is too big. Guess again: "
    elsif guess == number
      puts "Congragulations! You win!"
      print "Your "
      print guess_array.length
      print " incorrect guesses were: "

      if num_guess == 0
        sleep(0.5)
        print "... Oh."
      else
        while count_through < num_guess #Loop to relay user's guesses with a delay of 0.5 seconds
          print guess_array[count_through] 

          if count_through == num_guess - 2
            print ", and "
          elsif count_through == num_guess - 1
            puts ". "
          else 
            print ", "
          end

          count_through += 1
          sleep(0.5)     
        end

        puts "Would you like to play again? (yes/no)"
        replay_answer = gets.chomp
        until replay_answer == "yes" || replay_answer == "y" || replay_answer == "no" || replay_answer == "n" do
          print "Please answer with yes, y, no, or n: "
          replay_answer = gets.chomp
        end

        if replay_answer == "yes" || replay_answer == "y"
          replay = true
          puts "yes"
        elsif replay_answer == "no" || replay_answer == "n" #Determining whether or not to replay
          replay = false
          puts "no"
        end
      end
    end

    guess_array.push guess
    num_guess += 1
    #puts num_guess
    #puts guess_array[array_location]
    array_location += 1
    if num_guess >= 15 && guess != number
      puts "Sorry, you lost. "
      print "Your "
      print guess_array.size
      print " guesses were: "

      while count_through < num_guess
        print guess_array[count_through] #Same as loop above; for when player fails to guess correctly

        if count_through == num_guess - 2
          print ", and "
        elsif count_through == num_guess - 1
          puts ". "
        else 
          print ", "
        end 

        count_through += 1
        sleep(0.5)
      end

      puts "Would you like to play again? (yes/no)"
      replay_answer = gets.chomp
      until replay_answer == "yes" || replay_answer == "y" || replay_answer == "no" || replay_answer == "n" do
        print "Please answer with yes, y, no, or n: "
        replay_answer = gets.chomp
      end

      if replay_answer == "yes" || replay_answer == "y"
        replay = true
        replay_inner = true
        puts "yes"
      elsif replay_answer == "no" || replay_answer == "n" #Determining whether or not to replay
        replay = false
        replay_inner = false
        puts "no"
      end
    end
  end
4

3 回答 3

0

你,尤其是你的程序员同事发现这个和未来的错误的方式是正确地注释你的代码,例如:

# ask the user about an optional replay
replay_answer = gets.chomp

# only accept yes/y/no/n
until replay_answer == "yes" || replay_answer == "y" || replay_answer == "no" || replay_answer == "n" do
  print "Please answer with yes, y, no, or n: "
  replay_answer = gets.chomp
end

## recoded this to a case, as I think it's much nicer :)

# determining whether to replay or to stop the loop
case replay_answer
  when "yes", "y"
    replay = true
    puts "yes"
  when "no", "n"
    replay = false
    puts "no"

    replay_inner = false
end
于 2013-09-27T20:40:16.793 回答
0

我认为中的条件while应该是:

numberGuess<=14 && replay_inner == true
于 2013-09-27T19:46:46.577 回答
0

我已经修改了一些你的代码,你应该真正使用一些体面的编辑器,至少突出语法错误。我已更改ORAND内部循环中的条件(代码中的注释),并在猜测数字时添加了打破它的方法。我还删除了负责再次播放的代码的第二次出现,请记住,自己干燥。

replay = true
while replay
  p replay
  #Pre-var
  print "What difficulty? (1 for easy, 2 for medium, or 3 for hard): "
  difficulty = gets.chomp.to_i
  until difficulty == 1 || difficulty == 2 || difficulty == 3 do
    print "Please type either 1, 2, or 3: "
    difficulty = gets.chomp.to_i
  end

  #Variables
  if difficulty == 1
    r = Random.new
    number = r.rand(100..1000)
    puts "You have 15 guesses. "
    print "Guess a number with three digits: "
    within_num = 50
  elsif difficulty == 2
    r = Random.new
    number = r.rand(1000..10000)
    puts "You have 15 guesses. "
    print "Guess a number with four digits: "
    within_num = 500
  elsif difficulty == 3
    r = Random.new
    number = r.rand(10000..100000)
    puts "You have 15 guesses. "
    print "Guess a number with five digits: "
    within_num = 5000
  end
  guess = ""
  num_guess = 0
  guess_array = Array.new
  array_location = 0
  count_through = 0
  array_print = count_through - 1
  replay_inner = true

  #Body
  puts number
  while num_guess <= 14 && replay_inner == true #Keeping as <= 14 as to avoid unnecessarily rewriting code, still gives 15 guesses
    guess = gets.chomp.to_i
    if guess > number * 2
      print "That is more than double the number. Guess again: "
    elsif guess < number / 2
      print "That is less than half the number. Guess again: "
    elsif guess > number && guess < number + within_num #within_num: 50 for easy, 500 for medium, 5000 for hard
      print "You are close. That is too big. Guess again: "
    elsif guess < number && guess > number - within_num
      print "You are close. That is too small. Guess again: "
    elsif guess < number
      print "That is too small. Guess again: " #Hinting the user to how close they are.
    elsif guess > number
      print "That is too big. Guess again: "
    elsif guess == number
      puts "Congragulations! You win!"
      print "Your "
      print guess_array.length
      print " incorrect guesses were: "
      if num_guess == 0
        sleep(0.5)
        print "... Oh."
      else
        while count_through < num_guess #Loop to relay user's guesses with a delay of 0.5 seconds
          print guess_array[count_through]
          if count_through == num_guess - 2
            print ", and "
          elsif count_through == num_guess - 1
            puts ". "
          else
            print ", "
          end
          count_through += 1
          sleep(0.5)
        end
      end
      replay_inner = false # or just `break`, you have to break somehow from inner loop here
    end

    guess_array.push guess
    num_guess += 1
    #puts num_guess
    #puts guess_array[array_location]
    array_location += 1
    if num_guess >= 15 && guess != number
      puts "Sorry, you lost. "
      print "Your "
      print guess_array.size
      print " guesses were: "
      while count_through < num_guess
        print guess_array[count_through] #Same as loop above; for when player fails to guess correctly
        if count_through == num_guess - 2
          print ", and "
        elsif count_through == num_guess - 1
          puts ". "
        else
          print ", "
        end
        count_through += 1
        sleep(0.5)
      end
    end
  end



   puts "\nWould you like to play again? (yes/no)"
  replay_answer = gets.chomp
  until replay_answer == "yes" || replay_answer == "y" || replay_answer == "no" || replay_answer == "n" do
    print "Please answer with yes, y, no, or n: "
    replay_answer = gets.chomp
  end
  if replay_answer == "yes" || replay_answer == "y"
    replay = true
    replay_inner = true
    puts "yes"
  elsif replay_answer == "no" || replay_answer == "n" #Determining whether or not to replay
    replay = false
    replay_inner = false
    puts "no"
  end

end
于 2013-09-28T13:47:17.303 回答