From 9521e2aa55486842d4467cfdcca8b2446ec033b1 Mon Sep 17 00:00:00 2001 From: RealAnalysis Date: Sat, 18 Apr 2020 23:12:10 +0800 Subject: [PATCH 01/17] 01 --- 01-hello.rb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/01-hello.rb b/01-hello.rb index e0e7bbf..f0e0216 100644 --- a/01-hello.rb +++ b/01-hello.rb @@ -1,8 +1,7 @@ # 题目: 输入名字,输出 "Hello, 名字" print "请输入你的名字,然后按 Enter: " -your_name = gets -# ... +your_name = gets -puts "(请替换成最后的答案)" \ No newline at end of file +puts "Hello, #{your_name}" From 5c4ddfef85b7b49682692bbb2045b7fae2609c9b Mon Sep 17 00:00:00 2001 From: RealAnalysis Date: Sat, 18 Apr 2020 23:54:02 +0800 Subject: [PATCH 02/17] 02 --- 02-variable.rb | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/02-variable.rb b/02-variable.rb index a5a4753..d94a652 100644 --- a/02-variable.rb +++ b/02-variable.rb @@ -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),是指派右边的值到左边的变量” From 24b6f5431917e4d3c05cac5d080ea07b85ca2bb3 Mon Sep 17 00:00:00 2001 From: RealAnalysis Date: Mon, 20 Apr 2020 22:56:17 +0800 Subject: [PATCH 03/17] 04 --- 03-triangle.rb | 4 ++-- 04-pizzas.rb | 7 ++++--- 2 files changed, 6 insertions(+), 5 deletions(-) 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..d2c0cb4 100644 --- a/04-pizzas.rb +++ b/04-pizzas.rb @@ -6,7 +6,8 @@ print "请输入有多少人要吃,然后按 Enter: " people = gets -# ..... +a = pizzas.to_i +b = people.to_i -puts "每人可分得几片: _________ 片" -puts "还剩下几片: _________ 片" \ No newline at end of file +puts "每人可分得几片: #{a/b} 片" +puts "还剩下几片: #{a%b} 片" From d2abca52ce5992cb8e189f2382a104d1148e132c Mon Sep 17 00:00:00 2001 From: RealAnalysis Date: Sun, 26 Apr 2020 17:22:10 +0800 Subject: [PATCH 04/17] 05 --- 05-bmi.rb | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/05-bmi.rb b/05-bmi.rb index 67efdff..51be6c8 100644 --- a/05-bmi.rb +++ b/05-bmi.rb @@ -10,8 +10,18 @@ print "请输入您的身高(厘米),然后按 Enter: " height = gets -# ..... +b = height.to_f/100 -puts "您的 BMI 是: _________" +a = weight.to_f/(b * b) -puts "您的 BMI 结果是: _________(过轻或正常或过重)" \ No newline at end of file +if a < 18.5 + c = "过轻" +elsif a >= 24 + c = "过重" +else + c = "正常" +end + +puts "您的 BMI 是: #{a}" + +puts "您的 BMI 结果是: #{c}" From a1f3d0ba0dffd9d939219a3741f9af984b038d7f Mon Sep 17 00:00:00 2001 From: RealAnalysis Date: Sun, 26 Apr 2020 17:31:09 +0800 Subject: [PATCH 05/17] 06 --- 06-interger-positive.rb | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/06-interger-positive.rb b/06-interger-positive.rb index a240f5f..c6ad3d6 100644 --- a/06-interger-positive.rb +++ b/06-interger-positive.rb @@ -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 "这个数是_____ (偶数或奇数)" \ No newline at end of file +if x.to_i % 2 == 0 + b = "偶数" +else + b = "奇数" +end + +puts "这个数是#{a}" +puts "这个数是#{b}" From 1ed641d585cadc1372cae65e1516e18fcb319038 Mon Sep 17 00:00:00 2001 From: RealAnalysis Date: Sun, 26 Apr 2020 17:48:38 +0800 Subject: [PATCH 06/17] 07 --- 07-abcde.rb | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/07-abcde.rb b/07-abcde.rb index 5d0c8c3..d16ac81 100644 --- a/07-abcde.rb +++ b/07-abcde.rb @@ -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)" \ No newline at end of file +puts "结果是#{a}" From 870eadd3a2a26a6a121fcaf573a53c84a5528435 Mon Sep 17 00:00:00 2001 From: RealAnalysis Date: Sun, 26 Apr 2020 22:16:22 +0800 Subject: [PATCH 07/17] 08 --- 08-find-max.rb | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/08-find-max.rb b/08-find-max.rb index 9e6e643..42bb2b3 100644 --- a/08-find-max.rb +++ b/08-find-max.rb @@ -9,6 +9,18 @@ print "请输入一个数字z,然后按 Enter: " z = gets -# .... +if x > y + if x > z + c = "x" + else + c = "z" + end +else + if y > z + c = "y" + else y < z + c = "z" + end +end -puts "最大的数是 ________(x或y或z)" \ No newline at end of file +puts "最大的数是 #{c}" From 17793832e5e53bfdc66eab7ec058c126b884a47f Mon Sep 17 00:00:00 2001 From: RealAnalysis Date: Mon, 27 Apr 2020 17:25:27 +0800 Subject: [PATCH 08/17] 09 --- 09-function.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/09-function.rb b/09-function.rb index b1f922d..acc468e 100644 --- a/09-function.rb +++ b/09-function.rb @@ -1,7 +1,7 @@ # 题目: 输入直角三角形的宽和高,输出三角形的面积 def calculate_area(a, b) - # .... + a.to_f * b.to_f / 2 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}" From 5c935c1d23146bca758dfdd7a0d3f0c97d89930b Mon Sep 17 00:00:00 2001 From: RealAnalysis Date: Mon, 27 Apr 2020 18:26:54 +0800 Subject: [PATCH 09/17] modified 08 --- 08-find-max.rb | 8 ++++---- 10-function.rb | 16 ++++++++++++++-- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/08-find-max.rb b/08-find-max.rb index 42bb2b3..e80071b 100644 --- a/08-find-max.rb +++ b/08-find-max.rb @@ -9,16 +9,16 @@ print "请输入一个数字z,然后按 Enter: " z = gets -if x > y - if x > z +if x.to_f > y.to_f + if x.to_f > z.to_f c = "x" else c = "z" end else - if y > z + if y.to_f > z.to_f c = "y" - else y < z + else c = "z" end end diff --git a/10-function.rb b/10-function.rb index bb450fb..e677d23 100644 --- a/10-function.rb +++ b/10-function.rb @@ -1,6 +1,19 @@ # 题目: 使用者输入 x,y,z,请输出三个数中最大的数 def find_max(x, y, z) + if x > y + if x > z + c = "x" + else + c = "z" + end + else x < y + if y > z + c = "y" + else y < z + c = "z" + end + end end print "请输入一个数字x,然后按 Enter: " @@ -12,8 +25,7 @@ def find_max(x, y, z) print "请输入一个数字z,然后按 Enter: " z = gets -# .... answer = find_max(x,y,z) -puts "最大的数是 #{answer}" \ No newline at end of file +puts "最大的数是 #{answer}" From b54e770f983f7e6ed73e8b11bac2c550c91867c5 Mon Sep 17 00:00:00 2001 From: RealAnalysis Date: Mon, 27 Apr 2020 18:36:00 +0800 Subject: [PATCH 10/17] 10 --- 10-function.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/10-function.rb b/10-function.rb index e677d23..cb7faf5 100644 --- a/10-function.rb +++ b/10-function.rb @@ -1,16 +1,16 @@ # 题目: 使用者输入 x,y,z,请输出三个数中最大的数 def find_max(x, y, z) - if x > y - if x > z + if x.to_f > y.to_f + if x.to_f > z.to_f c = "x" else c = "z" end - else x < y - if y > z + else + if y.to_f > z.to_f c = "y" - else y < z + else c = "z" end end From 6286e719f76d2cad4111bdb82636f4574797993b Mon Sep 17 00:00:00 2001 From: RealAnalysis Date: Mon, 27 Apr 2020 18:59:04 +0800 Subject: [PATCH 11/17] modified 10 --- 10-function.rb | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/10-function.rb b/10-function.rb index cb7faf5..e31148e 100644 --- a/10-function.rb +++ b/10-function.rb @@ -3,15 +3,15 @@ def find_max(x, y, z) if x.to_f > y.to_f if x.to_f > z.to_f - c = "x" + return "x" else - c = "z" + return "z" end else if y.to_f > z.to_f - c = "y" + return "y" else - c = "z" + return "z" end end end @@ -26,6 +26,7 @@ def find_max(x, y, z) z = gets + answer = find_max(x,y,z) puts "最大的数是 #{answer}" From ba6f9a2302712ac0a50b3f185004b5fed40f1b41 Mon Sep 17 00:00:00 2001 From: RealAnalysis Date: Mon, 27 Apr 2020 22:33:57 +0800 Subject: [PATCH 12/17] 11 --- 11-seven.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/11-seven.rb b/11-seven.rb index 26c221d..3efc448 100644 --- a/11-seven.rb +++ b/11-seven.rb @@ -3,7 +3,10 @@ i = 1 while ( i <= 100 ) - # .... + if i % 7 == 0 + puts i + end i+=1 -end \ No newline at end of file + +end From 302558fbb3123ca8c489650d9270600dd9fd0f4c Mon Sep 17 00:00:00 2001 From: RealAnalysis Date: Mon, 27 Apr 2020 22:53:29 +0800 Subject: [PATCH 13/17] 12 --- 12-sum-even.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/12-sum-even.rb b/12-sum-even.rb index 73879bb..5a74ad9 100644 --- a/12-sum-even.rb +++ b/12-sum-even.rb @@ -5,9 +5,11 @@ while ( i <= 100 ) - # .... + if i % 2 == 0 + total += i + end i+=1 end -puts total \ No newline at end of file +puts total From a3c67bf366250f6d0f173f7bd769992a989903b9 Mon Sep 17 00:00:00 2001 From: RealAnalysis Date: Tue, 28 Apr 2020 23:56:59 +0800 Subject: [PATCH 14/17] 13 --- 13-nn.rb | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/13-nn.rb b/13-nn.rb index ac0a43b..6ae12de 100644 --- a/13-nn.rb +++ b/13-nn.rb @@ -3,9 +3,19 @@ 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 From c4a8e4d6ef5cb2453318b46b7ce3fcbab1e2513a Mon Sep 17 00:00:00 2001 From: RealAnalysis Date: Wed, 29 Apr 2020 00:03:54 +0800 Subject: [PATCH 15/17] if or while --- 13-nn.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/13-nn.rb b/13-nn.rb index 6ae12de..3be93e1 100644 --- a/13-nn.rb +++ b/13-nn.rb @@ -1,6 +1,7 @@ # 题目: 输入一个数字 N,输出 N * N 乘法表 print "请输入数字 N,然后按 Enter: " + n = gets i = 0 From 0695b6b0330e5915113f7304a9ef29f2b252ee44 Mon Sep 17 00:00:00 2001 From: RealAnalysis Date: Wed, 29 Apr 2020 20:51:13 +0800 Subject: [PATCH 16/17] 14 --- 14-prime.rb | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/14-prime.rb b/14-prime.rb index 8cf1692..bafd797 100644 --- a/14-prime.rb +++ b/14-prime.rb @@ -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 From 61ac7fe6321c8fefd9cfc8e771347a2532a5d469 Mon Sep 17 00:00:00 2001 From: RealAnalysis Date: Wed, 29 Apr 2020 21:28:39 +0800 Subject: [PATCH 17/17] 15 --- 15-guess-number.rb | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/15-guess-number.rb b/15-guess-number.rb index 48f9dca..cd15327 100644 --- a/15-guess-number.rb +++ b/15-guess-number.rb @@ -6,12 +6,15 @@ print "请猜一个 0~99 的数字 N,然后按 Enter: " n = gets - #puts "太低了,再猜一次" - #puts "太高了,再猜一次" + if n.to_i < target + puts "太低了,再猜一次" + elsif n.to_i > target + puts "太高了,再猜一次" + end if n.to_i == target puts "恭喜猜中啦! " break end -end \ No newline at end of file +end