Skip to content
5 changes: 2 additions & 3 deletions 01-hello.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# 题目: 输入名字,输出 "Hello, 名字"

print "请输入你的名字,然后按 Enter: "
your_name = gets

# ...
your_name = gets

puts "(请替换成最后的答案)"
puts "Hello, #{your_name}"
9 changes: 6 additions & 3 deletions 02-variable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
puts "a 是 #{a}"
puts "b 是 #{b}"

# ...
c = a
a = b
b = c

puts "a 应该是 2,现在是 #{a}"
puts "b 应该是 1,现在是 #{b}"
puts "a \"应该\"是 2,现在是 #{a}"
puts "b \"应该\"是 1,现在是 #{b}"

# 这才是真正的学习了,什么叫 “ ’=’ 是赋值运算符(assignment operator),是指派右边的值到左边的变量”
4 changes: 2 additions & 2 deletions 03-triangle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
print "请输入直角三角形的底边,然后按 Enter: "
b = gets

# .....
c = a.to_f * b.to_f / 2

puts "直角三角形的面积是: _________"
puts "直角三角形的面积是: #{c}"
7 changes: 4 additions & 3 deletions 04-pizzas.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
print "请输入有多少人要吃,然后按 Enter: "
people = gets

# .....
a = pizzas.to_i
b = people.to_i

puts "每人可分得几片: _________ 片"
puts "还剩下几片: _________ 片"
puts "每人可分得几片: #{a/b} 片"
puts "还剩下几片: #{a%b} 片"
16 changes: 13 additions & 3 deletions 05-bmi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,18 @@
print "请输入您的身高(厘米),然后按 Enter: "
height = gets

# .....
b = height.to_f/100

puts "您的 BMI 是: _________"
a = weight.to_f/(b * b)

puts "您的 BMI 结果是: _________(过轻或正常或过重)"
if a < 18.5
c = "过轻"
elsif a >= 24
c = "过重"
else
c = "正常"
end

puts "您的 BMI 是: #{a}"

puts "您的 BMI 结果是: #{c}"
18 changes: 15 additions & 3 deletions 06-interger-positive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,19 @@
print "请输入一个整数,然后按 Enter: "
x = gets

# ....
if x.to_i > 0
a = "正数"
elsif x.to_i < 0
a = "负数"
else
a = "零"
end

puts "这个数是_____ (正数或零或负数)"
puts "这个数是_____ (偶数或奇数)"
if x.to_i % 2 == 0
b = "偶数"
else
b = "奇数"
end

puts "这个数是#{a}"
puts "这个数是#{b}"
20 changes: 18 additions & 2 deletions 07-abcde.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,22 @@
print "请输入一个整数z,然后按 Enter: "
z = gets

# ....
if x.to_i < 0
a = "A"
else
if y.to_i > 0
if z.to_i > 0
a = "B"
else
a = "C"
end
else
if z.to_i >0
a = "D"
else
a = "E"
end
end
end

puts "结果是________(A或B或C或D或E)"
puts "结果是#{a}"
16 changes: 14 additions & 2 deletions 08-find-max.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@
print "请输入一个数字z,然后按 Enter: "
z = gets

# ....
if x.to_f > y.to_f
if x.to_f > z.to_f
c = "x"
else
c = "z"
end
else
if y.to_f > z.to_f
c = "y"
else
c = "z"
end
end

puts "最大的数是 ________(x或y或z)"
puts "最大的数是 #{c}"
4 changes: 2 additions & 2 deletions 09-function.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# 题目: 输入直角三角形的宽和高,输出三角形的面积

def calculate_area(a, b)
# ....
a.to_f * b.to_f / 2
end

print "请输入直角三角形的高,然后按 Enter: "
Expand All @@ -12,4 +12,4 @@ def calculate_area(a, b)

answer = calculate_area(a,b)

puts "直角三角形的面积是: #{answer}"
puts "直角三角形的面积是: #{answer}"
17 changes: 15 additions & 2 deletions 10-function.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
# 题目: 使用者输入 x,y,z,请输出三个数中最大的数

def find_max(x, y, z)
if x.to_f > y.to_f
if x.to_f > z.to_f
return "x"
else
return "z"
end
else
if y.to_f > z.to_f
return "y"
else
return "z"
end
end
end

print "请输入一个数字x,然后按 Enter: "
Expand All @@ -12,8 +25,8 @@ def find_max(x, y, z)
print "请输入一个数字z,然后按 Enter: "
z = gets

# ....


answer = find_max(x,y,z)

puts "最大的数是 #{answer}"
puts "最大的数是 #{answer}"
7 changes: 5 additions & 2 deletions 11-seven.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
i = 1
while ( i <= 100 )

# ....
if i % 7 == 0
puts i
end

i+=1
end

end
6 changes: 4 additions & 2 deletions 12-sum-even.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@

while ( i <= 100 )

# ....
if i % 2 == 0
total += i
end

i+=1
end

puts total
puts total
21 changes: 16 additions & 5 deletions 13-nn.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
# 题目: 输入一个数字 N,输出 N * N 乘法表

print "请输入数字 N,然后按 Enter: "

n = gets

# while ( ... )
# while ( ...)
#
# end
# end
i = 0

while ( i.to_i < n.to_i )

puts "#{i.to_i} x #{i.to_i} = #{i * i}"
puts "#{i.to_i} x #{i.to_i + 1} = #{i * (i + 1)}"
puts "#{i.to_i + 1} x #{i.to_i} = #{i * (i + 1)}"

i += 1

while ( i.to_i == n.to_i )
puts "#{i.to_i} x #{i.to_i} = #{i * i}"
break
end

end
14 changes: 13 additions & 1 deletion 14-prime.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
# 输入一个数字 N,请检查是不是质数

def is_prime(n)
# ....
i = 2

while i.to_i < n
a = n % i
if a.to_i == 0
break
else
i += 1
end
end

a != 0
end


print "请输入数字 N,然后按 Enter: "
n = gets

Expand Down
12 changes: 7 additions & 5 deletions 15-guess-number.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
# 题目 猜数字游戏:程序会先产生随机数,然后用户开始猜数字。程序会针对猜的数字回答太高、太低或猜中,猜中后程序就会终止。


target = rand(100)

while (true)
print "请猜一个 0~99 的数字 N,然后按 Enter: "
n = gets

#puts "太低了,再猜一次"
#puts "太高了,再猜一次"

if n.to_i == target
if n.to_i < target
puts "太低了,再猜一次"
elsif n.to_i > target
puts "太高了,再猜一次"
else n.to_i == target
puts "恭喜猜中啦! "
break
end

end
end
20 changes: 18 additions & 2 deletions 16-array-sum.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
# 给定一阵列内含数字,输出最大值

def find_max(array)
#....
i = 1
x = array[0]

while i < array.length
if x < array[i]
x = array[i]
end
i += 1
end

return x

end

# array.max

# array.each do |i|
# if x < i
# x = i

arr = [8, 12, 36, 53, 9, 75, 3, 71, 59, 88]

max = find_max(arr)
puts "Max is #{max}" # 应该是 88

16 changes: 12 additions & 4 deletions 17-array-stats.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,17 @@
end
end

i = 0
x = 0
while i < arr.size
x = x + arr[i]
i+=1
end


puts arr.to_s

puts "总和是 _____"
puts "平均是 _____"
puts "最大值是 _____"
puts "最小值是 _____"
puts "总和是 #{x}"
puts "平均是 #{(x.to_f/arr.size)}"
puts "最大值是 #{arr.max}"
puts "最小值是 #{arr.min}"
13 changes: 11 additions & 2 deletions 18-square.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@
print "请输入数字 N,然后按 Enter: "
n = gets

# ...
i = 0

puts arr.to_s
while i < n.to_i
if n == "\n"
break
else
arr << i * i
i += 1
end
end

puts arr.to_s
12 changes: 10 additions & 2 deletions 19-filter.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
# 给定一阵列内含数字,输出另一个数组只包含偶数

def filter_even(arr)
#...
array = []

for i in arr
if i % 2 == 0
array << i
end
end

puts array.to_s
end

arr = [7, 68, 42, 46, 9, 91, 77, 46, 86, 1]

puts filter_even(arr).to_s # 应该是 [68, 42, 46, 46, 86]
puts filter_even(arr).to_s # 应该是 [68, 42, 46, 46, 86]
Loading