【问题标题】:How can i make the loop end when user writes stop and to start a new number in the loop? [duplicate]当用户写停止并在循环中开始一个新数字时,如何使循环结束? [复制]
【发布时间】:2017-06-14 05:24:50
【问题描述】:

当用户写停止时,如何使以下循环结束,否则如果他们回答正确,如何再次调用该方法以使要猜测的数字不同? 游戏的想法是用户尝试从班级中获取数字,如果他们得到正确,那么游戏会询问他们是否想猜测班级生成的新数字或者他们是否想停止;如果是,他们会写停下来,游戏就结束了。 提前感谢您的帮助

class NumberGuessingGame
    #clase NumberGuessingGame
    def initialize
        #metodo que inicia
        @number= rand(0..9)
        #number es igual a un numero random entre 0 y 9
    end

def guess(numer)
        #metodo guess que dice que hay una condicion dada por el usuario, si no se da entonces se pide que el usuario la escriba manualmente
        if numer<@number
            #si el numero es mas pequeño que el numero entonces "Too low"
            "Too low"
        elsif numer>@number 
            #si el numero es mayor a el numero entonces "too high"
            "Too high"
        elsif numer == @number
            #si el numero es igual al numero random que pone la computadora entonces "you got it!"
            "you got it!"
        end
    end
end


game = NumberGuessingGame.new
# Pruebas
a = ""
p "Welcome to Guess the Number"
p "Human VS Machine"
while a != "Stop"
    x = ""
while x != "you got it!" 
    p"Write a number between 0 and 9"
    y = gets.chomp.to_i
    p x = game.guess(y)
end
p "WOOOOW!! Very Impresive. Want to defeat the machine again? If not write 
  stop or guess the new number"
  NumberGuessingGame
  a = gets.chomp
end

【问题讨论】:

  • 查看stackoverflow.com/questions/1402757/…。谈论打破循环。
  • 正常的方法是使用loop do &lt;your code&gt; end,如果一切正常,然后跳出循环(breakbreak x)。换句话说,假设你必须重复,除非没有必要这样做。

标签: ruby


【解决方案1】:

这是我为这个问题制作的代码解决方案。试试看是否合您的心意:

https://gist.github.com/BKSpurgeon/1a2346e278836d5b4448424cb93fd0e9

 class NumberGuessingGame    

    def initialize
        welcome
    end

    def welcome
        puts "Welcome to Guess the Number \n Human VS Machine"         
    end

 def guess 
    loop do       
        @number= rand(0..9)
        p "Write a number between 0 and 9"
        numer = -1
        while numer != @number
            numer = gets.chomp.to_i            
            if numer<@number                
                p "Too low"
            elsif numer>@number                 
                p "Too high"
            elsif numer == @number                
                p "You got it!"
                puts "WOOOOW!! Very Impresive. Want to defeat the machine again? If not write stop otherwise press any key."
                break
            end
        end
        answer = gets.chomp # The Critical Line to break out of the Loop is here:
        break if answer.downcase == "stop"
    end


 end
end

你会这样称呼它:

g = NumberGuessingGame.new
g.guess

如果你写 'stop' 就会转义。我对功能做了一些小的修改。如果检测到“停止”答案,则循环中断。那是关键线。

我看不出客户端代码做这种事情的充分理由:

game = NumberGuessingGame.new
# Pruebas
a = ""
p "Welcome to Guess the Number"
p "Human VS Machine"
while a != "Stop"
    x = ""
while x != "you got it!" 
    p"Write a number between 0 and 9"
    y = gets.chomp.to_i
    p x = game.guess(y)
end
p "WOOOOW!! Very Impresive. Want to defeat the machine again? If not write 
  stop or guess the new number"
  NumberGuessingGame
  a = gets.chomp
end

理想情况下,您希望让类中的所有方法完成所有工作 - 您不必在类外写“欢迎来到游戏等” - 这应该是 NumberGuessingGame 类的责任。

希望这会有所帮助。

【讨论】:

  • 非常感谢!!这完全解决了我的问题?
猜你喜欢
  • 1970-01-01
  • 2016-11-02
  • 2018-03-06
  • 1970-01-01
  • 1970-01-01
  • 2021-08-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多