Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions 01-hello.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
your_name = gets

# ...

puts "(请替换成最后的答案)"
print your_name + "is "
puts "smartful"
5 changes: 2 additions & 3 deletions 02-variable.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# 题目: 交换 a, b 变数的值

a = 1
b = 2
a = 7
b = 8

puts "a 是 #{a}"
puts "b 是 #{b}"
Expand All @@ -10,4 +10,3 @@

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

6 changes: 5 additions & 1 deletion 03-triangle.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# 题目: 使用者输入直角三角形的宽和高,输出三角形的面积



print "请输入直角三角形的高,然后按 Enter: "
a = gets

Expand All @@ -8,4 +10,6 @@

# .....

puts "直角三角形的面积是: _________"
puts "直角三角形的面积是: #{ a.to_f * b.to_f / 2 }"

#second time
4 changes: 2 additions & 2 deletions 04-pizzas.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@

# .....

puts "每人可分得几片: _________ 片"
puts "还剩下几片: _________ 片"
puts "每人可分得几片:#{ (pizzas.to_i / people.to_i).floor } 片"
puts "还剩下几片: #{ pizzas.to_i % people.to_i } "
15 changes: 12 additions & 3 deletions 05-bmi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,23 @@
# 如果 BMI >= 24,显示过重
# 如果 BMI 介于 18.5 ~ 24,显示正常


print "请输入您的体重(公斤),然后按 Enter: "
weight = gets

print "请输入您的身高(厘米),然后按 Enter: "
height = gets

# .....
BMI = (weight.to_f / (height.to_f/100) ** 2).round(2)

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

puts "您的 BMI 是: _________"
if BMI < 18.5
puts "您的 BMI 结果是: 过轻"
elsif BMI > 24
puts "您的 BMI 结果是: 过重"
else
puts "您的 BMI 结果是: 正常"
end

puts "您的 BMI 结果是: _________(过轻或正常或过重)"
#bug :输入任意非数字串
23 changes: 23 additions & 0 deletions 06-integer-positive.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# 题目: 输入一个数字 x,请判断是否正数、零或负数,以及是不是偶数


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

# ....

if x.to_f > 0
puts "正数"
elsif x.to_f < 0
puts "负数"
else
puts "0"
end

if x.to_f % 2 == 0
puts "偶数"
else
puts "奇数"
end

#bug :输入任意非数字串为0和偶数
10 changes: 0 additions & 10 deletions 06-interger-positive.rb

This file was deleted.

26 changes: 24 additions & 2 deletions 07-abcde.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,28 @@
print "请输入一个整数z,然后按 Enter: "
z = gets

# ....
q = x.to_f
w = y.to_f
e = z.to_f

puts "结果是________(A或B或C或D或E)"
if q < 0
puts "A"
elsif q > 0 && w > 0
if e > 0
puts "B"
elsif e < 0
puts "C"
else
puts "none1"
end
elsif q > 0 && w < 0
if e > 0
puts "D"
elsif e < 0
puts "E"
else
puts "none2"
end
else
puts "none3"
end
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

# ....
q = x.to_f
w = y.to_f
e = z.to_f

if q >= w && q >= e
puts "最大的数是 #{x}"
elsif w >= q && w >= e
puts "最大的数是 #{y}"
elsif e >= w && e >= q
puts "最大的数是 #{z}"
else
puts "none"
end

puts "最大的数是 ________(x或y或z)"
# ....
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}"
16 changes: 15 additions & 1 deletion 10-function.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
# 题目: 使用者输入 x,y,z,请输出三个数中最大的数

def find_max(x, y, z)
q = x.to_f
w = y.to_f
e = z.to_f

if q >= w && q >= e
return x
elsif w >= q && w >= e
return y
elsif e >= w && e >= q
return z
else
return "none"
end

end

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

answer = find_max(x,y,z)

puts "最大的数是 #{answer}"
puts "最大的数是 #{answer}"
7 changes: 4 additions & 3 deletions 11-seven.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

i = 1
while ( i <= 100 )

# ....
if (7*i) < 100
puts 7*i
end

i+=1
end
end
10 changes: 4 additions & 6 deletions 12-sum-even.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
# 题目: 求 1~100 所有偶数的和

i = 1
i = 0
total = 0

while ( i <= 100 )

# ....

i+=1
total += i
i+=2
end

puts total
puts total
19 changes: 14 additions & 5 deletions 13-nn.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,19 @@

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

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

while ( i <= n )
j = 0

while ( j <= n)

puts "#{i} * #{j} = #{i*j}"
j += 1

end

i += 1
end
17 changes: 16 additions & 1 deletion 14-prime.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
# 输入一个数字 N,请检查是不是质数

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

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

if is_prime(n.to_i)
puts "这是质数"
Expand Down
9 changes: 5 additions & 4 deletions 15-guess-number.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
print "请猜一个 0~99 的数字 N,然后按 Enter: "
n = gets

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

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

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

def find_max(array)
#....
max = array[0]
for i in 1..((array.length)-1)
if array[i] > max
max = array[i]
end
end

return max
end

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

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

puts "Max is #{max}" # 应该是 888
17 changes: 13 additions & 4 deletions 17-array-stats.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,18 @@
end
end

total = 0
for i in arr
total += i
end

average = total.to_f / arr.length
max = arr.max
min = arr.min

puts arr.to_s

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

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

# ...
for i in 0..n-1
arr << i**2
end

puts arr.to_s
puts arr.to_s
Loading