循环中的方法不循环 - Ruby

时间:2014-07-03 02:12:32

标签: ruby string loops methods

def playmany
    puts "Please enter in how many games you want to play"
    games_to_play = gets.chomp.to_i
    games_to_play.times do
        puts "\n"
        rules
    end
end

(绝对红宝石noob,如果我使用的条款不对,请提前道歉)

我想要重复方法'规则',但它只返回一次然后中断。有没有办法让'rules'方法执行用户指定的次数?

这就是'规则'包含的内容,我不知道它是'playmany'方法还是'规则'方法。

def rules
case player = rand(1..3)
    when player = 1
        player = "scissors"
        puts "Player choses: " + player
    when player = 2
        player = "paper"
        puts "Player choses: " + player
    when player = 3
        player = "rock"
        puts "Player choses: " + player
    end
case computer = rand(1..3)
    when computer = 1
        computer ="scissors"
        puts "Computer choses: " + computer
    when computer = 2
        computer ="paper"
        puts "Computer choses: " + computer
    when computer = 3
        computer = "rock"
        puts "Computer choses: " + computer
    end
case player
    when player = "scissors"
        if computer == "rock"
            puts "Player loses"
            $computertally += 1
            main_menu
        elsif computer == "scissors"
            puts "Computer and player tie"
            $tiecount += 1
            main_menu
        else
            puts "Player wins"
            $playertally += 1
            main_menu
        end
when player = "rock"
    if computer == "rock"
        puts "Computer and player ties"
        $tiecount += 1
        main_menu
    elsif computer == "scissors"
        puts "Player wins"
        $playertally += 1
        main_menu
    else
        puts "Player loses"
        $computertally += 1
        main_menu
    end
when player = "paper"
    if computer == "rock"
        puts "Player wins"
        $playertally += 1
        main_menu
    elsif computer == "scissors"
        puts "Player loses"
        $computertally += 1
        main_menu
    else
        puts "Computer and player tie"
        $tiecount += 1
        main_menu
    end
end
end

更新:main_menu就是这个

puts "Welcome to rock, paper, scissors" 
puts "--------------------------------"
puts "\n"
puts "What do you want to do?"
puts "-- Type 'play' to have a turn"
puts "-- Type 'playmany' to play multiple games"
puts "-- Type 'score' to see the current scoreboard"
puts "-- Type 'exit' to exit"
def main_menu
case gets.chomp.downcase
    when "play"
        rules
    when "score"
        puts "\n"
        puts "Computer has won #{$computertally} games"
        puts "Player has won #{$playertally} games"
        puts "A tie has occured #{$tiecount} times"
        puts "\n"
        main_menu
    when "exit"
        exit
    when "playmany"
        playmany
    else
        puts "Error with selection, please try again"
        main_menu
    end
end

3 个答案:

答案 0 :(得分:1)

您的问题应该正确关闭,因为SO不在这里调试您的代码。但由于这是你的第一个问题,我将做一个简短的回答。重新定义#rules方法如下:

def rules
  puts "Hi, I'm looping!"
end

现在,如果您调用#playmany方法,您会看到#rules方法实际 循环,但其中存在问题。考虑一下你的方法rules应该做什么,尝试为它编写测试,并尝试修改你的方法以满足测试期望。

答案 1 :(得分:1)

main_menu选项中,您实际上正在进入"退出" ?

退出确实退出程序。

尝试替换

when "exit"
exit

when "exit"
return

return将退出main_menu方法,并允许您的程序继续执行后续步骤,包括循环。

答案 2 :(得分:0)

很难说明这一点,但请确保您缩进代码,以便于阅读,并且所有循环和条件都以“结束”语句结束。

另外,尝试按如下方式调用规则 - “rules()”