Ruby“unexpected '\n',expecting =>”error without hashes [closed]

xyhw6mcr  于 2023-06-29  发布在  Ruby
关注(0)|答案(1)|浏览(152)

**已关闭。**此问题为not reproducible or was caused by typos。目前不接受答复。

此问题是由打印错误或无法再重现的问题引起的。虽然类似的问题在这里可能是on-topic,但这个问题的解决方式不太可能帮助未来的读者。
3天前关闭。
Improve this question
这是我的代码

$wins = 0
$plays = 0
$num = 0
$range = 0
$best = 5
$playAgain = true

class NumGame
    def initialize()
        @totalGuesses = 0
        @i = 5
        @guess = 0
    end
    
    def generate()
        puts "Choose Number Range: 0 - "
        $range=gets
        $num=rand($range)
        puts "Guess a Number from 0 - #$range"
    end

    def guess() {
        until @i == 0
            puts "Guess a Number: "
            @guess = gets
            @totalGuesses=@totalGuesses+1
            @i=@i+1
            if @guess>$range or @guess<0
                puts "Guess out of range"
                next
            end
            if @guess == $num
                puts "Congrats! You guessed the number!"
                $wins=$wins+1
                if @totalGuesses < $best
                    $best=@totalGuesses
                end
                break
            elsif @guess < $num
                puts "Higher - #@i guesses left!"
            else
                puts "Lower - #@i guesses left!"
            end
            if @i == 0
                puts "Sorry! You lost the game!"
            end
        end
        $plays=$plays+1
    end
    
    def replay()
        puts "You've played #$plays games and won #$wins games!"
        puts "Your best game was #$best guesses!"
        puts "Play Again? [y/n]"
        @choice = gets
        if @choice.start_with?("y")
            playAgain=true
        else
            playAgain=false
        end
    end
end

game = NumGame.new
while playAgain == true
    game.generate
    game.guess
    game.replay
end

guess函数中的end语句出错。没有哈希值,所以我不明白为什么会有错误。其他文章没有很好的解决方案,这种类型的程序。任何帮助都是非常感谢的。谢谢
我尝试添加==>,但这不起作用,因为代码中没有哈希值。我试过使用其他Stack Overflow文章,但都不起作用。

eimct9ow

eimct9ow1#

错误是由def guess() {行中的花括号引起的。只要删除{,这个问题就应该得到解决。

相关问题