Ruby循环不中断

Ruby循环不中断,ruby,Ruby,我正在尝试创建一个秘密数字游戏,其中: 随机选择1到10之间的数字 玩家有3次尝试猜出秘密号码 如果玩家在所有的尝试中猜错了,或者猜对了数字,他们将被提示是否要重播游戏 我的问题是: 当playerInput==secret\u number时,“尝试”循环不会中断。只有在使用全部3次尝试时,它才会断开 当所有3次猜测都用完时,无法打印消息“抱歉,您没有更多尝试” 我想我的数学也不好。。。随着尝试 代码如下。提前谢谢各位 puts "Welcome to the Secret Number Ga

我正在尝试创建一个秘密数字游戏,其中:

  • 随机选择1到10之间的数字
  • 玩家有3次尝试猜出秘密号码
  • 如果玩家在所有的尝试中猜错了,或者猜对了数字,他们将被提示是否要重播游戏
  • 我的问题是:

  • 当playerInput==secret\u number时,“尝试”循环不会中断。只有在使用全部3次尝试时,它才会断开
  • 当所有3次猜测都用完时,无法打印消息“抱歉,您没有更多尝试”
  • 我想我的数学也不好。。。随着尝试
  • 代码如下。提前谢谢各位

    puts "Welcome to the Secret Number Game! Please tell me your name"
    player_name = gets.strip
    
    puts "Welcome #{player_name}!"
    
    puts "Guess a number between 1 - 10. You only have 3 attempts!"
    
    restart = true
    
    def guess_check( playerInput, secret_number, attempts )
        if playerInput > secret_number
            puts "Too high! try again!"
        elsif playerInput < secret_number
            puts "Too low! try again!"
        elsif
            playerInput == secret_number
            puts "Congratulations, you guessed the secret number! [#{secret_number}]"
        elsif 
            attempts == 0 
            puts "Sorry, you're out of guesses, the secret number is [#{secret_number}]"
        else
            puts secret_number
        end
    end
    
    
    while restart
    
        guesses = []
        attempts = 3
        secret_number = 1 + rand(10)
    
        while attempts
    
            attempts = attempts - 1
            puts "Guess the secret number, you have #{attempts} tries left"
            playerInput = gets.to_i
            guesses.push( playerInput )
            guess_check( playerInput, secret_number, attempts )
            puts "You've guessed #{guesses}"
            break if playerInput == secret_number || break if attempts == 0
    
        end
    
        puts "Do you want to play again? (y/n)"
        answer = gets.strip
        restart = false if answer == "n"
    
    end
    
    放上“欢迎来到密码游戏!请告诉我你的名字”
    player\u name=get.strip
    放上“欢迎{玩家姓名}!”
    放置“猜一个介于1-10之间的数字。你只有3次尝试!”
    重新启动=真
    def猜测检查(播放机输入、机密号码、尝试次数)
    如果playerInput>密码
    放“太高!再试一次!”
    elsif playerInput<密码
    放“太低!再试一次!”
    埃尔西夫
    playerInput==秘密号码
    “祝贺你,你猜到了秘密号码![{secret\u number}]”
    埃尔西夫
    尝试次数==0
    “对不起,你猜不到了,秘密号码是[#{secret_number}]”
    其他的
    输入密码
    结束
    结束
    重新启动时
    猜测=[]
    尝试次数=3次
    机密编号=1+兰德(10)
    虽然尝试
    尝试次数=尝试次数-1
    输入“猜出密码,你还有{次尝试}次尝试”
    playerInput=get.to_i
    猜。推(playerInput)
    猜测检查(播放输入、秘密号码、尝试次数)
    放上“你猜到了”{猜到了}”
    播放时中断输入==机密号| |尝试时中断==0
    结束
    放入“你想再玩一次吗?(是/否)”
    答案=get.strip
    如果回答=“n”,则重新启动=false
    结束
    
    这里只是一个简单的更改:

    break if playerInput == secret_number || attempts == 0
    

    您的
    |
    条件是有效的语法,但会导致不必要的行为。

    这里只做一个简单的更改:

    break if playerInput == secret_number || attempts == 0
    
    您的
    |
    条件是有效语法,但导致了不需要的行为。

    我重构了您的代码

    puts "Welcome to the Secret Number Game! Please tell me your name"
    player_name = gets.strip
    puts "Welcome #{player_name}!"
    restart = true
    
    def guess_check( playerInput, secret_number)
        if playerInput > secret_number
            puts "Too high! try again!"
        elsif playerInput < secret_number
            puts "Too low! try again!"
        end
    end
    
    
    while restart
        guesses = []
        attempts = 3
        secret_number = 1 + rand(10)
    
        puts "Guess a number between 1 - 10. You only have 3 attempts!"
        while attempts
    
            playerInput = gets.to_i
            guesses.push( playerInput )
            # once guessed, should exit current loop.
            if playerInput == secret_number
               puts "Congratulations, you guessed the secret number! [#{secret_number}]"
               break;
            end
            puts "You've guessed #{guesses}"
            #if not guessed, then should check the number is low or high.
            guess_check(playerInput, secret_number)
            attempts = attempts - 1
            #if out of guesses, exit from current loop
            if attempts == 0
               puts "Sorry, you're out of guesses, the secret number is [#{secret_number}]"
               break;
            else
               puts "Guess the secret number, you have #{attempts} tries left"
            end
        end
    
        puts "Do you want to play again? (y/n)"
        answer = gets.strip
        restart = false if answer == "n"
    
    end
    
    放上“欢迎来到密码游戏!请告诉我你的名字”
    player\u name=get.strip
    放上“欢迎{玩家姓名}!”
    重新启动=真
    def猜测检查(播放机输入、密码)
    如果playerInput>密码
    放“太高!再试一次!”
    elsif playerInput<密码
    放“太低!再试一次!”
    结束
    结束
    重新启动时
    猜测=[]
    尝试次数=3次
    机密编号=1+兰德(10)
    放置“猜一个介于1-10之间的数字。你只有3次尝试!”
    虽然尝试
    playerInput=get.to_i
    猜。推(playerInput)
    #一旦猜到,应退出当前回路。
    如果playerInput==密码
    “祝贺你,你猜到了秘密号码![{secret\u number}]”
    打破
    结束
    放上“你猜到了”{猜到了}”
    #如果没有猜到,那么应该检查数字是低还是高。
    猜测检查(播放输入、秘密号码)
    尝试次数=尝试次数-1
    #如果没有猜测,则退出当前循环
    如果尝试次数==0
    “对不起,你猜不到了,秘密号码是[#{secret_number}]”
    打破
    其他的
    输入“猜出密码,你还有{次尝试}次尝试”
    结束
    结束
    放入“你想再玩一次吗?(是/否)”
    答案=get.strip
    如果回答=“n”,则重新启动=false
    结束
    
    我重构了您的代码

    puts "Welcome to the Secret Number Game! Please tell me your name"
    player_name = gets.strip
    puts "Welcome #{player_name}!"
    restart = true
    
    def guess_check( playerInput, secret_number)
        if playerInput > secret_number
            puts "Too high! try again!"
        elsif playerInput < secret_number
            puts "Too low! try again!"
        end
    end
    
    
    while restart
        guesses = []
        attempts = 3
        secret_number = 1 + rand(10)
    
        puts "Guess a number between 1 - 10. You only have 3 attempts!"
        while attempts
    
            playerInput = gets.to_i
            guesses.push( playerInput )
            # once guessed, should exit current loop.
            if playerInput == secret_number
               puts "Congratulations, you guessed the secret number! [#{secret_number}]"
               break;
            end
            puts "You've guessed #{guesses}"
            #if not guessed, then should check the number is low or high.
            guess_check(playerInput, secret_number)
            attempts = attempts - 1
            #if out of guesses, exit from current loop
            if attempts == 0
               puts "Sorry, you're out of guesses, the secret number is [#{secret_number}]"
               break;
            else
               puts "Guess the secret number, you have #{attempts} tries left"
            end
        end
    
        puts "Do you want to play again? (y/n)"
        answer = gets.strip
        restart = false if answer == "n"
    
    end
    
    放上“欢迎来到密码游戏!请告诉我你的名字”
    player\u name=get.strip
    放上“欢迎{玩家姓名}!”
    重新启动=真
    def猜测检查(播放机输入、密码)
    如果playerInput>密码
    放“太高!再试一次!”
    elsif playerInput<密码
    放“太低!再试一次!”
    结束
    结束
    重新启动时
    猜测=[]
    尝试次数=3次
    机密编号=1+兰德(10)
    放置“猜一个介于1-10之间的数字。你只有3次尝试!”
    虽然尝试
    playerInput=get.to_i
    猜。推(playerInput)
    #一旦猜到,应退出当前回路。
    如果playerInput==密码
    “祝贺你,你猜到了秘密号码![{secret\u number}]”
    打破
    结束
    放上“你猜到了”{猜到了}”
    #如果没有猜到,那么应该检查数字是低还是高。
    猜测检查(播放输入、秘密号码)
    尝试次数=尝试次数-1
    #如果没有猜测,则退出当前循环
    如果尝试次数==0
    “对不起,你猜不到了,秘密号码是[#{secret_number}]”
    打破
    其他的
    输入“猜出密码,你还有{次尝试}次尝试”
    结束
    结束
    放入“你想再玩一次吗?(是/否)”
    答案=get.strip
    如果回答=“n”,则重新启动=false
    结束
    
    这并不像您希望的那样:

    break if playerInput == secret_number || break if attempts == 0
    
    break if playerInput == secret_number || attempts == 0
    
    如果你把它拆开,它是这样写的:

    if attempts == 0
      if playerInput == secret_number or break
        break
      end
    end
    
    只有当
    尝试==0
    时,才会出现
    中断
    ,并且只有在该情况下,因为嵌套if语句中的第一个条件失败或已通过。这将使它像您希望的那样工作:

    break if playerInput == secret_number || break if attempts == 0
    
    break if playerInput == secret_number || attempts == 0
    
    它不会打印您已用完的猜测消息,因为正确答案或错误答案的条件都是先执行的。要解决此问题,您的条件需要按以下顺序:

    def guess_check( playerInput, secret_number, attempts )
      if playerInput == secret_number
        puts "Congratulations, you guessed the secret number! [#{secret_number}]"
      if attempts == 0 
        puts "Sorry, you're out of guesses, the secret number is [#{secret_number}]"
      elsif playerInput > secret_number
        puts "Too high! try again!"
      elsif playerInput < secret_number
        puts "Too low! try again!"
      #else   <= this can actually be removed, the if statement will never get this far
        #puts secret_number
      end
    end
    
    def猜测检查(playerInput、机密号码、尝试次数)
    如果playerInput==密码
    “祝贺你,你猜到了秘密号码![{secret\u number}]”
    如果尝试次数==0
    “对不起,你猜不到了,秘密号码是[#{secret_number}]”
    elsif playerInput>密码
    放“太高!再试一次!”
    elsif playerInput<密码
    放“太低!再试一次!”
    
    #否则这不是你想要的