溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Ruby-條件判斷

發布時間:2020-08-19 18:57:19 來源:網絡 閱讀:669 作者:3581758abc 欄目:編程語言
=begin
      條件判斷語句包括if語句,unless語句,case語句,
=end

#if 語句
=begin
if 條件 then
  處理1
elsif 條件2 then
  處理2
else
  處理3
end
=end
a = 10
b = 20
if a > b
  puts "a bigger than b"
elsif a < b
  puts "a smaller than b"
else
  puts "a is equal to b"
end
puts "a bigger than b" if a > b

#unless 語句,與if語句相反,條件判斷為假的時執行處理
=begin
unless 條件
  處理1
else
  處理2
end
=end
a = 10
b = 20
unless a > b
  puts "a smaller/same as than b"
end

#case 語句,適用于比較的對象只有一個的時候,根據這個對象的值不同,執行不同的處理;when可以一次指定多個值
=begin
case 比較對象
when 值1 then
  處理1
when 值2 then
  處理2
when 值3 then
  處理3
else
  處理4
end
=end
tags = ["A", "IMG", "PRE"]
tags.each do |word|
  case word
  when "P", "A", "I", "B", "BLOCKQUOTE"
    puts "#{word} has child."
  when "IMG", "BR"
    puts "#{word} has child."
  else
    puts "#{word} cannoot be used"
  end
end

array = ["a", 1, nil]
array.each do |word|
  case word
  when String
    puts "the word is a String"
  when Numeric 
    puts "the word is a Numeric"
  else
    puts "other words are Something"
  end
end

text.each_line do |line|
  case line
  when /^From:/i
    puts "find a sender"
  when /^To:/i
    puts "find a receiver"
  when /^$/
    puts "resolution are finnished"
    exit
  else
    break
  end
end


向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女