This post is Private. Only a writer or those who know its URL can access this post.

Improve article
RevisionsShow article in Markdown
Report article
Help us understand the problem. What is going on with this article?

Rubyでやるプログラミングのドリル

More than 1 year has passed since last update.

問題1

実行結果を考えて下さい。

a = 1
b = 2
c = a + b
c = c + 1

puts c

問題2

実行結果を考えて下さい。

a = 8
b = 2
c = a - b
puts c

c = c / b
puts c

問題3

実行結果を考えて下さい。

a = 6
b = 2
c = a * b
puts c

c = c + 1
puts c

問題4

実行結果を考えて下さい。

a = 6
b = 2
c = a * b
puts c

c = c / a
puts c

問題5

実行結果を考えて下さい。

a = 6
b = 2
c = a * b
puts c

c = c / (a - b) 
puts c

問題6

実行結果を考えて下さい。

age = 0
price = 0

if age == 0
  price = 100
else
  price = 500
end

puts price

問題7

実行結果を考えて下さい。

age = 5
price = 0

if age == 0
  price = 100
else
  price = 500
end

puts price

問8

実行結果を考えて下さい。

age = 5
price = 0

if age == 0
  price = 100
end

puts price

問題9

実行結果を考えて下さい。

age = 1
price = 0

if age == 0
  price = 100
elsif age == 1
  price = 300
else
  price = 500
end

puts price

問題10

実行結果を考えて下さい。

age = 0
price = 0

if age > 0
  price = 100
else
  price = 500
end

puts price

問題11

実行結果を考えて下さい。

age = 0
price = 0

if age >= 0
  price = 100
else
  price = 500
end

puts price

問題12

実行結果を考えて下さい。

age = 0
price = 0

if age < 1
  price = 100
else
  price = 500
end

puts price

問題13

実行結果を考えて下さい。

age = 2
price = 0

if age <= 1
  price = 100
else
  price = 500
end

puts price

問題14

実行結果を考えて下さい。

age = 2
birth = 2000
price = 0

if age == 2 && birth == 2001
  price = 100
else
  price = 500
end

puts price

問題15

実行結果を考えて下さい。

age = 2
birth = 2000
price = 0

if age == 2 || birth == 2001
  price = 100
else
  price = 500
end

puts price

問題16

実行結果を考えて下さい。

age = 2
birth = 2000
price = 0

if age == 2 && birth == 2000
  price = 100
else
  price = 500
end

puts price

問題17

実行結果を考えて下さい。

age = 2
birth = 2000
price = 0

if age == 2 || birth == 2000
  price = 100
else
  price = 500
end

puts price

問題18

実行結果を考えて下さい。

age = 2
birth = 2000
price = 0

if age == 2
  if birth == 2001
    price = 100
  else
    price = 300
  end
else
  price = 500
end

puts price

問題19

実行結果を考えて下さい。
電卓を使ってもいいです。

money = 1000
money = money * 1.1
money = money * 1.1
money = money * 1.1

puts money.to_i

問題20

実行結果を考えて下さい。
電卓を使ってもいいです。

money = 1000
1.upto(3) do
  money = money * 1.1
end
puts money.to_i

問題21

実行結果を考えて下さい。
電卓を使ってもいいです。

money = 1000
rate = 1.1
1.upto(4) do
  money = money * rate
end
puts money.to_i

問題22

実行結果を考えて下さい。
電卓を使ってもいいです。

money = 1000
rate = 1.1
2001.upto(2004) do
  money = money * rate
end
puts money.to_i

問題23

実行結果を考えて下さい。
電卓を使ってもいいです。

money = 1000
rate = 1.1
(2001..2010).step(2) do
  money = money * rate
end
puts money.to_i

問題24

実行結果を考えて下さい。

money = 1000
rate = 1.1
(2001..2010).step(3) do
  money = money * rate
end
puts money.to_i

問題25

実行結果を考えて下さい。

money = 1000
(1..10).each do
  (1..10).each do
    money = money + 1
  end
end

puts money

問題26

実行結果を考えて下さい。

val = "cebu"
puts "Hello #{val} welcome"

問題27

実行結果を考えて下さい。

val = 1
puts "Hello #{1 * 10} welcome"

問題28

実行結果を考えて下さい。

money = 1000
rate = 1.1
2001.upto(2004) do |i|
  money = money * rate
  puts "I got #{money.to_i} in #{i}."
end

問題29

実行結果を考えて下さい。

(1..10).each do |i|
  (1..10).each do |v|
    puts "i = #{i}"
    puts "v = #{v}"
  end
end

問題30

実行結果を考えて下さい。

(1..10).each do |i|
  (1..10).each do |v|
    puts "i = #{i}" if i % 2 == 0
    puts "v = #{v}" if v % 2 == 1
  end
end

問題31

実行結果を考えて下さい。

def print_hello(v)
  puts "hello, #{v}"
end

(1..10).each do |i|
  print_hello(i)
end

問題32

実行結果を考えて下さい。

a = 0
b = 0

(1..10).each do
  if a > b
    b = b + 1
    puts "1; a = #{a}, b = #{b}"
  else
    a = a + 1
    puts "2; a = #{a}, b = #{b}"
  end
end

問題33

実行結果を考えて下さい。

def display_price(price)
  case price
  when 100 then
    size = "small"
  when 200 then
    size = "mid"
  when 300 then
    size = "large"
  else
    size = "LL"
  end

  print "size is #{size}"
end

display_price(100)
display_price(200)
display_price(300)
display_price(400)

問題34

実行結果を考えて下さい。

def display_price(price)
  size = case price
    when 100 then
      "small"
    when 200 then
      "mid"
    when 300 then
      "large"
    else
      "LL"
   end

  print "size is #{size}"
end

display_price(100)
display_price(200)
display_price(300)
display_price(400)

問題35

実行結果を考えて下さい。

def cal_bmi(height, weight)
  if height > 180
    bmi = weight / (height * 0.01) ** 2
    puts "You are tall. This is your #{bmi}"
  elsif weight > 80
    bmi = weight / (height * 0.01) ** 2
    puts "You are big. This is your #{bmi}"
  elsif height < 160 && weight < 50
    bmi = weight / (height * 0.01) ** 2
    puts "You are small. This is your #{bmi}"
  else
    bmi = weight / (height * 0.01) ** 2
    puts "You are normal. This is your #{bmi}"
  end  
end

cal_bmi(187, 65)
cal_bmi(180, 90)
cal_bmi(159, 49)
cal_bmi(159, 60)

問題36

実行結果を考えて下さい。

def cal_bmi(height, weight)
  bmi = weight / (height * 0.01) ** 2
  message = 
    if height > 180
      "You are tall. This is your #{bmi}"
    elsif weight > 80
      "You are big. This is your #{bmi}"
    elsif height < 160 && weight < 50
      "You are small. This is your #{bmi}"
    else
      "You are normal. This is your #{bmi}"
    end
  puts message
end

cal_bmi(187, 65)
cal_bmi(180, 90)
cal_bmi(159, 49)
cal_bmi(159, 60)

問題37

実行結果を考えて下さい。

def cal_bmi(height, weight)
  bmi = weight / (height * 0.01) ** 2
  message = 
    if height > 180
      "tall"
    elsif weight > 80
      "big"
    elsif height < 160 && weight < 50
      "small"
    else
      "normal"
    end
  puts "You are #{message}. This is your #{bmi}"
end

cal_bmi(187, 65)
cal_bmi(180, 90)
cal_bmi(159, 49)
cal_bmi(159, 60)
joji
I worked in Philippines and work in Tokyo.
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away