Skip to content
2 changes: 1 addition & 1 deletion 01-hello.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@

# ...

puts "(请替换成最后的答案)"
puts "hello #{your_name}"
7 changes: 5 additions & 2 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}"

5 changes: 4 additions & 1 deletion 03-triangle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
print "请输入直角三角形的底边,然后按 Enter: "
b = gets

c = a.to_f * b.to_f

c = c / 2
# .....

puts "直角三角形的面积是: _________"
puts "直角三角形的面积是: __#{c}___"
8 changes: 6 additions & 2 deletions 04-pizzas.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,9 @@

# .....

puts "每人可分得几片: _________ 片"
puts "还剩下几片: _________ 片"
a=pizzas.to_i/people.to_i

b=pizzas.to_i%people.to_i

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

w = weight.to_f
h = height.to_f/100
b = w/h
b = b/h

if b < 18.5
a = "过轻"
elsif b >= 24
a = "过重"
else
a = "正常"
end
# .....

puts "您的 BMI 是: _________"
puts "您的 BMI 是: ____#{b}_____"

puts "您的 BMI 结果是: _________(过轻或正常或过重)"
puts "您的 BMI 结果是: ____#{a}_____(过轻或正常或过重)"
19 changes: 16 additions & 3 deletions 06-interger-positive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,21 @@

print "请输入一个整数,然后按 Enter: "
x = gets

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

if x%2 != 0
b = "奇数"
else
b = "偶数"
end

puts "这个数是_____ (正数或零或负数)"
puts "这个数是_____ (偶数或奇数)"
puts "这个数是__#{a}___ (正数或零或负数)"
puts "这个数是___#{b}__ (偶数或奇数)"
23 changes: 22 additions & 1 deletion 07-abcde.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,27 @@
print "请输入一个整数z,然后按 Enter: "
z = gets

x = x.to_i
y = y.to_i
z = z.to_i

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

puts "结果是________(A或B或C或D或E)"
puts "结果是____#{a}____(A或B或C或D或E)"
14 changes: 13 additions & 1 deletion 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

x = x.to_f
y = y.to_f
z = z.to_f

if x >= y && x >= z
a = "x"
elsif y >= x && y >= z
a = "y"
else
a = "z"
end

# ....

puts "最大的数是 ________(x或y或z)"
puts "最大的数是 ___#{a}_____(x或y或z)"
3 changes: 2 additions & 1 deletion 09-function.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

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

answer = calculate_area(a,b)

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

def find_max(x, y, z)
x=x.to_f
y=y.to_f
z=z.to_f

if x>y && y>z
x
elsif y>z
y
else
z
end
end

print "请输入一个数字x,然后按 Enter: "
Expand All @@ -16,4 +27,4 @@ def find_max(x, y, z)

answer = find_max(x,y,z)

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

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

puts i

end

i+=1
end
end
8 changes: 6 additions & 2 deletions 12-sum-even.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@

while ( i <= 100 )

# ....
if i%2 == 0

total = total + i

end

i+=1
end

puts total
puts total
18 changes: 13 additions & 5 deletions 13-nn.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,17 @@
print "请输入数字 N,然后按 Enter: "
n = gets

# while ( ... )
# while ( ...)
#
# end
# end
n = n.to_i

a=1
b=1

while (a<=n)
while (b<=n)
print "#{a*b} "
b=b+1
end
b=1
a=a+1
puts
end
10 changes: 10 additions & 0 deletions 14-prime.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
# 输入一个数字 N,请检查是不是质数

def is_prime(n)
n=n.to_i
i=2
while i<=n/2
if n % i ==0
return false
else
i = i + 1
end
end
true
# ....
end

Expand Down
13 changes: 8 additions & 5 deletions 15-guess-number.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
while (true)
print "请猜一个 0~99 的数字 N,然后按 Enter: "
n = gets

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

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

end
end
10 changes: 9 additions & 1 deletion 16-array-sum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@

def find_max(array)
#....
i=0
m=0
while i <(array.length)
if m<array[i]
m=array[i]
end
i=i+1
end
m
end

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

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

13 changes: 9 additions & 4 deletions 17-array-stats.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,15 @@
arr << user_input.to_i
end
end
total = 0
arr.each{|a| total += a}
ave = total.to_f/arr.length
max = arr.max
min = arr.min

puts arr.to_s

puts "总和是 _____"
puts "平均是 _____"
puts "最大值是 _____"
puts "最小值是 _____"
puts "总和是 __#{total}___"
puts "平均是 ___#{ave}__"
puts "最大值是 ___#{max}__"
puts "最小值是 ___#{min}__"
9 changes: 7 additions & 2 deletions 18-square.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@

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

n=n.to_i
i = 0
while i<=n
arr[i]=i ** 2
i=i+1
end
# ...

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

def filter_even(arr)
#...
newarr = []
for a in arr
if a%2==0
newarr.push(a)
end
end
newarr
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]
10 changes: 9 additions & 1 deletion 20-sorting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,17 @@

def filter_even(arr)
#...
newarr = []
for a in arr
if a%2==0
newarr.push(a)
end
end
newarr.sort.uniq

end

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


puts "________" # 应该是 [42, 46, 68, 86]
puts "___#{filter_even(arr)}" # 应该是 [42, 46, 68, 86]
Loading