小编kun*_*nal的帖子

有没有更好的方法在if语句中编写多个OR语句?

def get_string(no_of_times)
    1.upto(no_of_times) do 
    string_input = gets.chomp
    count_holes(string_input)
    end 
end

def count_holes(word)
    count = 0
    word.each_char do |char|
        if char == "A" || char == "D" || char == "O" || char == "P" || char == "Q" || char == "R" 
            count += 1
        elsif char == "B"
            count += 2
        end
    end
    $arr_of_holes << count
end



test_cases = gets.chomp.to_i

$arr_of_holes = []
get_string(test_cases)
puts $arr_of_holes
Run Code Online (Sandbox Code Playgroud)

大家好.迭代每个字符时,我不喜欢if语句中的长条件.所以我想问你们所有人是否有更好的办法在红宝石中做到这一点.

谢谢

ruby if-statement

3
推荐指数
1
解决办法
154
查看次数

标签 统计

if-statement ×1

ruby ×1