0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Ruby の偶奇判定したい時に使う evenと odd について

0
Posted at

even と odd とは?

Ruby の整数(Integer)が持っているメソッドで、

even? → 偶数なら true

odd? → 奇数なら true

を返します。

使い方

4.even?   # => true
7.even?   # => false

5.odd?    # => true
10.odd?   # => false

偶奇判定の実装例

if を使う書き方

n = gets.to_i

if n.even?
  puts "even"
else
  puts "odd"
end

Ruby らしい短い書き方(三項演算子)

n = gets.to_i
puts n.even? ? "even" : "odd"
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?