diff --git a/01-hello.rb b/01-hello.rb index e0e7bbf..33fbd61 100644 --- a/01-hello.rb +++ b/01-hello.rb @@ -2,7 +2,4 @@ print "请输入你的名字,然后按 Enter: " your_name = gets - -# ... - -puts "(请替换成最后的答案)" \ No newline at end of file +puts "hello, #{your_name}" diff --git a/02-variable.rb b/02-variable.rb index a5a4753..53b4c95 100644 --- a/02-variable.rb +++ b/02-variable.rb @@ -6,8 +6,9 @@ puts "a 是 #{a}" puts "b 是 #{b}" -# ... +c = a +a = b +b = c puts "a 应该是 2,现在是 #{a}" puts "b 应该是 1,现在是 #{b}" - diff --git a/03-triangle.rb b/03-triangle.rb index fafec03..00e3c5e 100644 --- a/03-triangle.rb +++ b/03-triangle.rb @@ -6,6 +6,6 @@ print "请输入直角三角形的底边,然后按 Enter: " b = gets -# ..... +c = a.to_f * b.to_f / 2 -puts "直角三角形的面积是: _________" \ No newline at end of file +puts "直角三角形的面积是: #{c}" diff --git a/04-pizzas.rb b/04-pizzas.rb index 4c2521f..b140762 100644 --- a/04-pizzas.rb +++ b/04-pizzas.rb @@ -6,7 +6,8 @@ print "请输入有多少人要吃,然后按 Enter: " people = gets -# ..... +c = pizzas.to_i / people.to_i +d = pizzas.to_i % people.to_i -puts "每人可分得几片: _________ 片" -puts "还剩下几片: _________ 片" \ No newline at end of file +puts "每人可分得几片: #{c} 片" +puts "还剩下几片: #{d}片" diff --git a/05-bmi.rb b/05-bmi.rb index 67efdff..0f28694 100644 --- a/05-bmi.rb +++ b/05-bmi.rb @@ -10,8 +10,22 @@ print "请输入您的身高(厘米),然后按 Enter: " height = gets -# ..... +h = height.to_f / 100 -puts "您的 BMI 是: _________" +BMI = (weight.to_f / (h * h)) -puts "您的 BMI 结果是: _________(过轻或正常或过重)" \ No newline at end of file +def c + if BMI < 18.5 + c = "过轻" + else if + BMI >= 24 + c = "过重" + else c = "正常" + end + end +end + + +puts "您的 BMI 是: #{BMI}" + +puts "您的 BMI 结果是: #{c}(过轻或正常或过重)" diff --git a/06-interger-positive.rb b/06-interger-positive.rb index a240f5f..4896af0 100644 --- a/06-interger-positive.rb +++ b/06-interger-positive.rb @@ -2,9 +2,17 @@ print "请输入一个整数,然后按 Enter: " -x = gets +x = gets.to_i -# .... +if x > 0 + puts "这个数是正数" + else if x ==0 + puts "这个数是0" + else puts "这个数是负数" +end +end -puts "这个数是_____ (正数或零或负数)" -puts "这个数是_____ (偶数或奇数)" \ No newline at end of file +if x % 2 ==0 + puts "这个数是偶数" +else puts "这个数是奇数" +end diff --git a/07-abcde.rb b/07-abcde.rb index 5d0c8c3..44f43c4 100644 --- a/07-abcde.rb +++ b/07-abcde.rb @@ -9,14 +9,33 @@ # 当 z < 0 输出 "E" print "请输入一个整数x,然后按 Enter: " -x = gets +x = gets.to_f print "请输入一个整数y,然后按 Enter: " -y = gets +y = gets.to_f print "请输入一个整数z,然后按 Enter: " -z = gets +z = gets.to_f -# .... +if x < 0 + puts "结果是A" +end -puts "结果是________(A或B或C或D或E)" \ No newline at end of file +if x > 0 + if y > 0 + if z > 0 + puts "结果是B" + elsif z < 0 + puts "结果是C" + end + end + + if y < 0 + if z > 0 + puts "结果是D" + elsif z < 0 + puts "结果是E" + end + end + +end diff --git a/08-find-max.rb b/08-find-max.rb index 9e6e643..3768d5a 100644 --- a/08-find-max.rb +++ b/08-find-max.rb @@ -9,6 +9,14 @@ print "请输入一个数字z,然后按 Enter: " z = gets -# .... +if x > y && x > z + puts "最大的数是#{x}" +end -puts "最大的数是 ________(x或y或z)" \ No newline at end of file +if y > x && y > z + puts "最大的数是#{y}" +end + +if z > x && z > y + puts "最大的数是#{z}" +end diff --git a/09-function.rb b/09-function.rb index b1f922d..6801e49 100644 --- a/09-function.rb +++ b/09-function.rb @@ -1,7 +1,7 @@ # 题目: 输入直角三角形的宽和高,输出三角形的面积 def calculate_area(a, b) - # .... + calculate_area = a.to_f * b.to_f end print "请输入直角三角形的高,然后按 Enter: " @@ -12,4 +12,4 @@ def calculate_area(a, b) answer = calculate_area(a,b) -puts "直角三角形的面积是: #{answer}" \ No newline at end of file +puts "直角三角形的面积是: #{answer}" diff --git a/10-function.rb b/10-function.rb index bb450fb..9756441 100644 --- a/10-function.rb +++ b/10-function.rb @@ -1,19 +1,25 @@ -# 题目: 使用者输入 x,y,z,请输出三个数中最大的数 +# 题目: 使用者输入 x,y,z,请输出三个数中最大的数。使用回传值表示 def find_max(x, y, z) + if x > z && x > y + return x + elsif y > z && y > x + return y + elsif z > x && z > y + return z + end end print "请输入一个数字x,然后按 Enter: " -x = gets +x = gets.to_f print "请输入一个数字y,然后按 Enter: " -y = gets +y = gets.to_f print "请输入一个数字z,然后按 Enter: " -z = gets +z = gets.to_f -# .... answer = find_max(x,y,z) -puts "最大的数是 #{answer}" \ No newline at end of file +puts "最大的数是 #{answer}" diff --git a/11-seven.rb b/11-seven.rb index 26c221d..7c45c11 100644 --- a/11-seven.rb +++ b/11-seven.rb @@ -2,8 +2,8 @@ i = 1 while ( i <= 100 ) - - # .... - - i+=1 -end \ No newline at end of file + if i % 7 == 0 + puts i + end + i+= 1 +end diff --git a/12-sum-even.rb b/12-sum-even.rb index 73879bb..fe7befc 100644 --- a/12-sum-even.rb +++ b/12-sum-even.rb @@ -4,10 +4,10 @@ total = 0 while ( i <= 100 ) - - # .... - + if i % 2 ==0 + total = total + i + end i+=1 end -puts total \ No newline at end of file +puts total diff --git a/13-nn.rb b/13-nn.rb index ac0a43b..cd5cbab 100644 --- a/13-nn.rb +++ b/13-nn.rb @@ -1,11 +1,16 @@ # 题目: 输入一个数字 N,输出 N * N 乘法表 print "请输入数字 N,然后按 Enter: " -n = gets +n = gets.to_i +i = 0 +j = 0 -# while ( ... ) -# while ( ...) -# -# end -# end +while (i <= n) + j = i + while (j <= n) + puts "#{i} * #{j} = #{i*j}" + j += 1 + end + i += 1 +end diff --git a/14-prime.rb b/14-prime.rb index 8cf1692..ce4ad8a 100644 --- a/14-prime.rb +++ b/14-prime.rb @@ -1,14 +1,23 @@ # 输入一个数字 N,请检查是不是质数 def is_prime(n) -# .... + i = 2 + while (i <= n/2 + 1) + if n % i != 0 + return true + else + return false + end + i += 1 + end + end -print "请输入数字 N,然后按 Enter: " +print "请输入一个数字N,然后按 enter:" n = gets if is_prime(n.to_i) - puts "这是质数" + puts "这是质数" else - puts "这不是质数" + puts "这不是质数" end diff --git a/15-guess-number.rb b/15-guess-number.rb index 48f9dca..2766f50 100644 --- a/15-guess-number.rb +++ b/15-guess-number.rb @@ -6,12 +6,12 @@ print "请猜一个 0~99 的数字 N,然后按 Enter: " n = gets - #puts "太低了,再猜一次" - #puts "太高了,再猜一次" - - if n.to_i == target - puts "恭喜猜中啦! " - break + if n.to_i > target + puts "太高了,再猜一次" + elsif n.to_i < target + puts "太低了,再猜一次" + elsif n.to_i == target + puts "恭喜猜中啦! " + break end - -end \ No newline at end of file +end diff --git a/16-array-sum.rb b/16-array-sum.rb index 9b4910b..1da8378 100644 --- a/16-array-sum.rb +++ b/16-array-sum.rb @@ -1,11 +1,16 @@ # 给定一阵列内含数字,输出最大值 def find_max(array) - #.... + m = array[0] + array.each do |i| + if m <= i + m = i + end + end + return m end arr = [8, 12, 36, 53, 9, 75, 3, 71, 59, 88] max = find_max(arr) puts "Max is #{max}" # 应该是 88 -