【问题标题】:Ruby while loop not stopping when condition is met满足条件时Ruby while循环不会停止
【发布时间】:2016-11-08 10:10:11
【问题描述】:

我开始学习 Ruby,但我一直坚持我正在编写的这个程序,它被认为是游戏“掷骰子”游戏的一小部分,至少无论如何,代码在第二个 while 循环中循环当随机骰子满足循环条件时。

这是不断循环的循环

while new_dice_sum != 7 or new_dice_sum != 11 or new_dice_sum != 2 or new_dice_sum != 3 or new_dice_sum != 12 or new_dice_sum != point
                    dice1 = 1 + rand(6)
                    dice2 = 1 + rand(6)
                    new_dice_sum = dice1.to_i + dice2.to_i

这是程序的完整代码。

input = ""
point = 0
balance = 100
Bet = 10
dice1 = 0
dice2 = 0
dice_sum = 0
new_dice_sum = 0
won = 0
lost = 0
while input != "Q"
    print "Please choose R to roll the dice or Q to exit the game: "
    input = gets.chomp.upcase
    if input == "R"
        if balance > 10
            dice1 = 1 + rand(6)
            dice2 = 1 + rand(6)
            dice_sum = dice1.to_i + dice2.to_i
            puts "First dice is #{dice1} while the second dice is #{dice2} both are a total of #{dice_sum}"
            if dice_sum == 7 || dice_sum == 11
                balance = balance + Bet
                won = won + 1
                puts "You won, Your new balance is #{balance}"
            elsif dice_sum == 2 || dice_sum == 3 || dice_sum == 12
                balance = balance - Bet
                lost = lost + 1
                puts "You lost, Your new balance is #{balance}"
            else
                point = dice_sum
                while new_dice_sum != 7 or new_dice_sum != 11 or new_dice_sum != 2 or new_dice_sum != 3 or new_dice_sum != 12 or new_dice_sum != point
                    dice1 = 1 + rand(6)
                    dice2 = 1 + rand(6)
                    new_dice_sum = dice1.to_i + dice2.to_i
                    puts "In the new roll with your point #{point}, The first dice is #{dice1} while the second dice is #{dice2} both are a total of #{new_dice_sum}"
                    if new_dice_sum == 7 || new_dice_sum == 11
                        balance = balance + Bet
                        won = won + 1
                        puts "You won, Your new balance is #{balance}"
                    elsif new_dice_sum == 2 || new_dice_sum == 3 || new_dice_sum == 12
                        balance = balance - Bet
                        lost = lost + 1
                        puts "You lost, Your new balance is #{balance}"
                    elsif new_dice_sum == point 
                        balance = balance + Bet
                        won = won + 1 
                        puts "Your Total dice is equal to your point you won, your new balance is #{balance}"
                    end
                end
            end  
        else
            puts "Your balance is not enough to place a bet, Your balance now is #{balance}"
        end
    elsif input != "Q"
    puts "#{input} is not valid choice"
    end
end
puts "Your current balance is #{balance}$ and you won #{won} times and lost #{lost} times"
exit

【问题讨论】:

  • 如果你想停止循环,你应该调用return。而且你的代码使用嵌套的 if 和循环也很复杂。
  • 如果您有答案,我建议关闭帖子或将答案标记为已接受。不是每个人都会阅读评论部分。

标签: ruby loops while-loop


【解决方案1】:

因为你对所有参数都做,所以即使第一个匹配它也会通过不匹配第二个。

variable != firstcondition or variable != secondcondition 更改您的 while 循环逻辑 ... 至 !(variable == condition or variable == condition ... )

【讨论】:

  • 呃...你可能想要==,而不是=。就个人而言,我只是建议while ![7,11,2,3,12].include?(variable)
  • 大声笑,是的,ty,更新了答案,不确定 ruby​​ 语法是否正在查看逻辑
  • 我的印象是,如果任何一个 or 返回 true,循环就会结束。
  • 不,如果任何一个 or 产生 TRUTH,它将进入循环,例如如果 new_dice_sum 为 7,那么第一次检查产生 FALSE 但 or 表示继续检查,下一条语句 (7 != 11) 不是 TRUE
  • @sary 不,如果ors 链中的任何析取项返回真,则整个链为真,因此循环继续。只有当 all 的析取项都为 false 时,循环才会结束。
猜你喜欢
  • 2019-01-29
  • 2013-11-22
  • 2019-05-16
  • 1970-01-01
  • 2016-03-14
  • 2020-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多