处理Ruby Case语句

时间:2017-07-16 13:42:26

标签: ruby

我尝试用“case”语句替换下面一段代码中的“if / else语句”,并且我坚持了几个小时 - 我错过了什么?

puts "Welcome to 'Guess My Number!'"
print "What is your name?"
input = gets
name = input.chomp

puts "Welcome, #{name.upcase!}!"
puts "I've got a random number between 1 and 100!"
puts "Can you guess it?"
target = rand(100) + 1 

num_guesses = 0
guessed_it = false
until num_guesses == 10 || guessed_it
    remaining_guesses = 10 - num_guesses
    puts "You've got #{remaining_guesses.to_s} guesses left!"
    print "Make a guess, put down a number: "
    guess = gets.chomp.to_i
    num_guesses = num_guesses + 1
end

puts case verification
    when guess < target 
        then "Ooops. Your guess was LOW."
    when guess > target
        then "Ooops. Your guess was HIGH."
    when guess < -1 
        then puts "Oooops. You have entered a number lower that 1!"
    when guess > 100
        then puts "Oooops. You have entered a number higher than 100!"
    when guess =~ /^([w])/
        then puts "Ooops. Looks like you have entered a non numeric 
value!"
    when guess == String
        then puts "Oooops! Looks like you have entered a non numeric 
value"

    when guess == target 
        then puts "Good job, #{name}!"  
             puts "You guessed my number in #{num_guesses} guesses!"
             guessed_it = true
end

unless guessed_it
    puts "Sorry, you didn't get my number. My number was #{target}."
end

“case statement”用于替换和增强以下if else语句的逻辑:

if guess < target
    puts "Ooops. Your guess was LOW."
 elsif guess > target
  puts "Ooops. Your guess was HIGH."
 elsif guess == target 
  puts "Good job, #{name}!" 
  puts "You guessed my number in #{num_guesses} guesses!"
  guessed_it = true
 end

3 个答案:

答案 0 :(得分:2)

您的问题是,您使用的case表单中包含可选条件,但您使用的是when子句,就好像您使用的是无条件case一样。

puts case
     when guess < target 
       "Ooops. Your guess was LOW."

应该有用。

进一步说明: 在没有条件的情况下使用case,执行具有truthy表达式的最早when分支。这就是你想要的。

但您使用caseverification。在这种情况下,将所有分支与verification进行比较,并执行verification === branch condition为真的第一个分支。 由于在您的示例中我猜测verification总是nil,并且所有分支的条件始终为truefalse,因此不会执行任何分支。

答案 1 :(得分:0)

您可以使用TPanel语句,如下所示:

case

答案 2 :(得分:0)

非常感谢大家!通过你宝贵的帮助,我设法从这个小任务中重新获得了我的灵魂和满足感的耐心:)我的错误是我试图以错误的顺序运行几段代码而违反了常识规则。我在case循环中移动了case语句,现在我所要做的就是纠正错误,尤其是当/ then语句时。它有效:)

    until num_guesses == 10 || guessed_it
    remaining_guesses = 10 - num_guesses
    puts "You've got #{remaining_guesses.to_s} guesses left!"
    print "Make a guess, put down a number: "
    guess = gets.chomp.to_i
    num_guesses = num_guesses + 1

        puts case 
        when guess < target 
            then "Ooops. Your guess was LOW."
        when guess > target
            then "Ooops. Your guess was HIGH."
        when guess < -1 
            then puts "Oooops. You have entered a number lower that 1!"
        when guess > 100
            then puts "Oooops. You have entered a number higher than 100!"
        when guess =~ /^([w])/
            then puts "Ooops. Looks like you have entered a non numeric value!"
        when guess == String
            then puts "Oooops! Looks like you have entered a non numeric value"

        when guess == target 
            then puts "Good job, #{name}!"  
                 puts "You guessed my number in #{num_guesses} guesses!"
        guessed_it = true
    end
end

unless guessed_it
    puts "Sorry, you didn't get my number. My number was #{target}."
end