From 93517c6629f3812e4236c27b3f99a949e6a87ded Mon Sep 17 00:00:00 2001 From: liyangbin Date: Mon, 28 Aug 2017 20:28:26 +0800 Subject: [PATCH 01/28] 01 --- 01-hello.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/01-hello.rb b/01-hello.rb index e0e7bbf..7a6fccc 100644 --- a/01-hello.rb +++ b/01-hello.rb @@ -5,4 +5,4 @@ # ... -puts "(请替换成最后的答案)" \ No newline at end of file +puts "hollo, #{your_name}" From 8094a57476acb81310855907fcb1525471195ee0 Mon Sep 17 00:00:00 2001 From: liyangbin Date: Mon, 28 Aug 2017 20:35:16 +0800 Subject: [PATCH 02/28] 02 --- 02-variable.rb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/02-variable.rb b/02-variable.rb index a5a4753..6c977db 100644 --- a/02-variable.rb +++ b/02-variable.rb @@ -8,6 +8,5 @@ # ... -puts "a 应该是 2,现在是 #{a}" -puts "b 应该是 1,现在是 #{b}" - +puts "a 应该是 #{a},现在是 #{b}" +puts "b 应该是 #{b},现在是 #{a}" From d857a9d7f721d0606224500b19c8a850d890fc7e Mon Sep 17 00:00:00 2001 From: liyangbin Date: Mon, 28 Aug 2017 20:50:21 +0800 Subject: [PATCH 03/28] 03 --- 03-triangle.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/03-triangle.rb b/03-triangle.rb index fafec03..ccedf19 100644 --- a/03-triangle.rb +++ b/03-triangle.rb @@ -7,5 +7,5 @@ b = gets # ..... - -puts "直角三角形的面积是: _________" \ No newline at end of file +c = a.to_i * b.to_i / 2 +puts "直角三角形的面积是: #{c}" From a924395e3830fd0ec401bbc32f2c0ae85fe15e43 Mon Sep 17 00:00:00 2001 From: liyangbin Date: Mon, 28 Aug 2017 20:56:24 +0800 Subject: [PATCH 04/28] 04 --- 04-pizzas.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/04-pizzas.rb b/04-pizzas.rb index 4c2521f..d4dca43 100644 --- a/04-pizzas.rb +++ b/04-pizzas.rb @@ -8,5 +8,7 @@ # ..... -puts "每人可分得几片: _________ 片" -puts "还剩下几片: _________ 片" \ No newline at end of file +a = pizzas.to_i / people.to_i +b = pizzas.to_i % people.to_i +puts "每人可分得几片: #{a} 片" +puts "还剩下几片: #{b} 片" From 8fe888f50a1dc61569fe7696a1aa94af6b7378f6 Mon Sep 17 00:00:00 2001 From: liyangbin Date: Mon, 28 Aug 2017 21:20:37 +0800 Subject: [PATCH 05/28] 05 --- 05-bmi.rb | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/05-bmi.rb b/05-bmi.rb index 67efdff..f63db98 100644 --- a/05-bmi.rb +++ b/05-bmi.rb @@ -12,6 +12,18 @@ # ..... -puts "您的 BMI 是: _________" +a = weight.to_i +b = height.to_f / 100 +c = a / (b * b) -puts "您的 BMI 结果是: _________(过轻或正常或过重)" \ No newline at end of file +if c < 18.5 + d = "过轻" +elsif c >= 24 + d = "过重" +else d = "正常" + +end + +puts "您的 BMI 是: #{c}" + +puts "您的 BMI 结果是: #{d}" From b86bcc596618294634574935ec714df6d2964a05 Mon Sep 17 00:00:00 2001 From: liyangbin Date: Mon, 28 Aug 2017 21:48:37 +0800 Subject: [PATCH 06/28] 06 --- 06-interger-positive.rb | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/06-interger-positive.rb b/06-interger-positive.rb index a240f5f..d0c2ed8 100644 --- a/06-interger-positive.rb +++ b/06-interger-positive.rb @@ -5,6 +5,24 @@ x = gets # .... +a = x.to_i -puts "这个数是_____ (正数或零或负数)" -puts "这个数是_____ (偶数或奇数)" \ No newline at end of file +if a > 0 + b = "正数" +elsif a == 0 + b = "零" +else b = "负数" + +end +if a > 0 + +if a % 2 == 0 + c = "偶数" +else + c = "奇数" +end + +end + +puts "这个数是: #{b}" +puts "这个数是: #{c}" From 8232e270cb6de89465dd8e82076e294e9efc598d Mon Sep 17 00:00:00 2001 From: liyangbin Date: Mon, 28 Aug 2017 22:11:42 +0800 Subject: [PATCH 07/28] 07 --- 07-abcde.rb | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/07-abcde.rb b/07-abcde.rb index 5d0c8c3..32cb39f 100644 --- a/07-abcde.rb +++ b/07-abcde.rb @@ -11,6 +11,7 @@ print "请输入一个整数x,然后按 Enter: " x = gets + print "请输入一个整数y,然后按 Enter: " y = gets @@ -19,4 +20,23 @@ # .... -puts "结果是________(A或B或C或D或E)" \ No newline at end of file +a = x.to_f +b = y.to_f +c = z.to_f + +if a < 0 + h = "A" +elsif a > 0 && b > 0 && c > 0 + h = "B" + elsif a > 0 && b > 0 && c < 0 + h = "C" + elsif a > 0 && b < 0 && c > 0 + h = "D" + elsif a > 0 && b < 0 && c < 0 + h = "E" +end + + + + +puts "结果是:#{h}" From d92facec7fe9e413436e030cbce37bae1563f54b Mon Sep 17 00:00:00 2001 From: liyangbin Date: Mon, 28 Aug 2017 22:36:43 +0800 Subject: [PATCH 08/28] 08 --- 08-find-max.rb | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/08-find-max.rb b/08-find-max.rb index 9e6e643..dca086c 100644 --- a/08-find-max.rb +++ b/08-find-max.rb @@ -11,4 +11,21 @@ # .... -puts "最大的数是 ________(x或y或z)" \ No newline at end of file +x = x.to_f +y = y.to_f +z = z.to_f + +# if x > y && x > z +# h = "x" +# elsif y > x && y > z +# h = "y" +# elsif z > x && z > y +# h = "z" +# else +# puts "无法求解" +# +# end + +h = [x, y, z].max.to_i + +puts "最大的数是: #{h}" From e9867911ad926b2f4a00f9a1f8dd0dbab5f941cd Mon Sep 17 00:00:00 2001 From: liyangbin Date: Tue, 29 Aug 2017 21:01:23 +0800 Subject: [PATCH 09/28] 09 --- 09-function.rb | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/09-function.rb b/09-function.rb index b1f922d..9e760c5 100644 --- a/09-function.rb +++ b/09-function.rb @@ -1,15 +1,18 @@ # 题目: 输入直角三角形的宽和高,输出三角形的面积 def calculate_area(a, b) - # .... + a * b / 2 end print "请输入直角三角形的高,然后按 Enter: " -a = gets +a = gets.to_i print "请输入直角三角形的底边,然后按 Enter: " -b = gets +b = gets.to_i + + + answer = calculate_area(a,b) -puts "直角三角形的面积是: #{answer}" \ No newline at end of file +puts "直角三角形的面积是: #{answer}" From 3d3bbd77f5b8318c555b29b42d6676a8b6a8ca19 Mon Sep 17 00:00:00 2001 From: liyangbin Date: Tue, 29 Aug 2017 21:04:10 +0800 Subject: [PATCH 10/28] 10 --- 10-function.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/10-function.rb b/10-function.rb index bb450fb..0177819 100644 --- a/10-function.rb +++ b/10-function.rb @@ -1,6 +1,7 @@ # 题目: 使用者输入 x,y,z,请输出三个数中最大的数 def find_max(x, y, z) + [x, y, z].max end print "请输入一个数字x,然后按 Enter: " @@ -16,4 +17,4 @@ def find_max(x, y, z) answer = find_max(x,y,z) -puts "最大的数是 #{answer}" \ No newline at end of file +puts "最大的数是 #{answer}" From 12556cc9776e63d73ba0b2b81959afc5be32f1f5 Mon Sep 17 00:00:00 2001 From: liyangbin Date: Tue, 29 Aug 2017 21:10:57 +0800 Subject: [PATCH 11/28] 11 --- 11-seven.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/11-seven.rb b/11-seven.rb index 26c221d..2bfa19d 100644 --- a/11-seven.rb +++ b/11-seven.rb @@ -2,8 +2,11 @@ i = 1 while ( i <= 100 ) + if i % 7 == 0 + puts i + end # .... i+=1 -end \ No newline at end of file +end From 240b632caad1737fe04be29587cd2f2ee74e713b Mon Sep 17 00:00:00 2001 From: liyangbin Date: Tue, 29 Aug 2017 21:19:29 +0800 Subject: [PATCH 12/28] 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..34f975c 100644 --- a/12-sum-even.rb +++ b/12-sum-even.rb @@ -4,10 +4,12 @@ total = 0 while ( i <= 100 ) - + if i % 2 == 0 + total += i + end # .... i+=1 end -puts total \ No newline at end of file +puts total From f89f4481cc04ffb7e1e17b63b6e2745961f2faa4 Mon Sep 17 00:00:00 2001 From: liyangbin Date: Tue, 29 Aug 2017 21:34:03 +0800 Subject: [PATCH 13/28] 13 --- 13-nn.rb | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/13-nn.rb b/13-nn.rb index ac0a43b..f146bb6 100644 --- a/13-nn.rb +++ b/13-nn.rb @@ -1,11 +1,21 @@ # 题目: 输入一个数字 N,输出 N * N 乘法表 print "请输入数字 N,然后按 Enter: " -n = gets +n = gets.to_i # while ( ... ) # while ( ...) # # end # end +i = 0 +while (i <= n) + j = 0 + while (j <= n) + h = i * j + puts "#{i} × #{j} = #{h}" + j += 1 + end + i += 1 +end From 31bf44d5c07daa5ed733dad7430bfd97898d6357 Mon Sep 17 00:00:00 2001 From: liyangbin Date: Tue, 29 Aug 2017 21:55:22 +0800 Subject: [PATCH 14/28] 14 --- 14-prime.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/14-prime.rb b/14-prime.rb index 8cf1692..14d7b7c 100644 --- a/14-prime.rb +++ b/14-prime.rb @@ -2,6 +2,15 @@ def is_prime(n) # .... + i = 2 + while (i <= n/2) + if n % i == 0 + return false + end + i += 1 + end + return true + end print "请输入数字 N,然后按 Enter: " From e1ba34f4093a78b0e61b4d31b7c13526c63d571a Mon Sep 17 00:00:00 2001 From: liyangbin Date: Tue, 29 Aug 2017 22:04:16 +0800 Subject: [PATCH 15/28] 15 --- 15-guess-number.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/15-guess-number.rb b/15-guess-number.rb index 48f9dca..2be5fc1 100644 --- a/15-guess-number.rb +++ b/15-guess-number.rb @@ -8,10 +8,14 @@ #puts "太低了,再猜一次" #puts "太高了,再猜一次" + if n.to_i < target + puts "太低了,再猜一次" + elsif n.to_i > target + puts "太高了,再猜一次" - if n.to_i == target + else n.to_i == target puts "恭喜猜中啦! " break end -end \ No newline at end of file +end From 6c67a7622f9693ec374cd3a872bcf66e3f1523bc Mon Sep 17 00:00:00 2001 From: liyangbin Date: Tue, 29 Aug 2017 22:18:11 +0800 Subject: [PATCH 16/28] 16 --- 16-array-sum.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/16-array-sum.rb b/16-array-sum.rb index 9b4910b..5260142 100644 --- a/16-array-sum.rb +++ b/16-array-sum.rb @@ -2,10 +2,16 @@ def find_max(array) #.... + # array.max + b = 0 + array.each do |a| + if b < a + b = a + end + end end arr = [8, 12, 36, 53, 9, 75, 3, 71, 59, 88] max = find_max(arr) puts "Max is #{max}" # 应该是 88 - From 2c2c802cfce001bc8f715815b3895d37b13c44c7 Mon Sep 17 00:00:00 2001 From: liyangbin Date: Wed, 30 Aug 2017 21:17:46 +0800 Subject: [PATCH 17/28] 17 --- 17-array-stats.rb | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/17-array-stats.rb b/17-array-stats.rb index 0af81bb..cc030c5 100644 --- a/17-array-stats.rb +++ b/17-array-stats.rb @@ -12,9 +12,16 @@ end end +sum = 0 +arr.each { |i| sum += i} +average = sum/arr.size.to_f + + + + puts arr.to_s -puts "总和是 _____" -puts "平均是 _____" -puts "最大值是 _____" -puts "最小值是 _____" \ No newline at end of file +puts "总和是 #{sum}" +puts "平均是 #{average}" +puts "最大值是 #{arr.max}" +puts "最小值是 #{arr.min}" From fab47ee3a3a8b74c489043210b2a0b186a194469 Mon Sep 17 00:00:00 2001 From: liyangbin Date: Wed, 30 Aug 2017 21:26:07 +0800 Subject: [PATCH 18/28] 18 --- 18-square.rb | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/18-square.rb b/18-square.rb index 226e1c1..d51dd9a 100644 --- a/18-square.rb +++ b/18-square.rb @@ -3,8 +3,17 @@ arr = [] print "请输入数字 N,然后按 Enter: " -n = gets +n = gets.to_i # ... +# i = 0 +# while i < n +# arr << i * i +# i += 1 +# end -puts arr.to_s \ No newline at end of file +(0..n).each_with_index do |i, j| + arr << j ** 2 +end + +puts arr.to_s From ccf302ddfbfcb8519010e9e4088fdbe793cf4546 Mon Sep 17 00:00:00 2001 From: liyangbin Date: Wed, 30 Aug 2017 21:41:53 +0800 Subject: [PATCH 19/28] 19 --- 19-filter.rb | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/19-filter.rb b/19-filter.rb index ef7e515..44e7e17 100644 --- a/19-filter.rb +++ b/19-filter.rb @@ -2,8 +2,16 @@ def filter_even(arr) #... + new_array = [] + + arr.each do |i| + if i % 2 == 0 + new_array << i + end + end + print "#{new_array}" end arr = [7, 68, 42, 46, 9, 91, 77, 46, 86, 1] -puts filter_even(arr).to_s # 应该是 [68, 42, 46, 46, 86] \ No newline at end of file +puts filter_even(arr).to_s # 应该是 [68, 42, 46, 46, 86] From 5dc0c7a8f7d190f30c01de0f98c11a0dbeccce05 Mon Sep 17 00:00:00 2001 From: liyangbin Date: Wed, 30 Aug 2017 21:49:55 +0800 Subject: [PATCH 20/28] 20 --- 20-sorting.rb | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/20-sorting.rb b/20-sorting.rb index 5f82c08..163937f 100644 --- a/20-sorting.rb +++ b/20-sorting.rb @@ -2,10 +2,19 @@ # Hint: 可用 arr.sort 排序,和 arr.uniq 去除重复 def filter_even(arr) - #... + new_array = [] + + arr.each do |i| + if i % 2 == 0 + new_array << i + end + end + print "#{new_array.sort.uniq}" end + + arr = [7, 68, 42, 46, 9, 91, 77, 46, 86, 1] -puts "________" # 应该是 [42, 46, 68, 86] \ No newline at end of file +puts "#{filter_even(arr)}" # 应该是 [42, 46, 68, 86] From a36406f626f015b8fa2510adc8098afc3d51b89f Mon Sep 17 00:00:00 2001 From: liyangbin Date: Wed, 30 Aug 2017 22:30:26 +0800 Subject: [PATCH 21/28] 21 --- 21-selection-sort.rb | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/21-selection-sort.rb b/21-selection-sort.rb index e5e7eae..320347d 100644 --- a/21-selection-sort.rb +++ b/21-selection-sort.rb @@ -3,10 +3,37 @@ def selection_sort(arr) #... + +# i = 0 +# j = 1 +# s = arr.size.to_i +# while i <= s - 1 +# min = arr[i] +# while j <= s - 1 +# if min > arr[j] +# arr[i] = arr[j] +# arr[j] = min +# min = arr[i] +# end +# j += 1 +# end +# i += 1 +# j = i + 1 +# end +# arr +# end + + (0...arr.size).each do |i| + min, index = arr[i], i + (i...arr.size).each { |j| min, index = arr[j], j if arr[j] < min } + arr[i], arr[index] = arr[index], arr[i] + end + arr end + arr = [7, 68, 42, 46, 9, 91, 77, 46, 86, 1] answer = selection_sort(arr) -puts answer.to_s # 应该是 [1, 7, 9, 42, 46, 46, 68, 77, 86, 91] \ No newline at end of file +puts answer.to_s # 应该是 [1, 7, 9, 42, 46, 46, 68, 77, 86, 91] From a4b4dec4971841841b6637d3402470bd6b021790 Mon Sep 17 00:00:00 2001 From: liyangbin Date: Wed, 30 Aug 2017 22:36:55 +0800 Subject: [PATCH 22/28] 22 --- 22-missing.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/22-missing.rb b/22-missing.rb index 6898714..a810164 100644 --- a/22-missing.rb +++ b/22-missing.rb @@ -2,6 +2,11 @@ def find_missing(arr) # ... + new_array = [] + (0..9).each do |i| + new_array << i unless arr.include?(i) + end + new_array end answer = find_missing( [2,2,1,5,8,4] ) From 8ff94787dddbdd319d257ace50531c0d42a6e073 Mon Sep 17 00:00:00 2001 From: liyangbin Date: Wed, 30 Aug 2017 22:43:22 +0800 Subject: [PATCH 23/28] 23 --- 23-hash-max.rb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/23-hash-max.rb b/23-hash-max.rb index 6fb227e..dfca6fc 100644 --- a/23-hash-max.rb +++ b/23-hash-max.rb @@ -1,7 +1,8 @@ # 给定一 Hash,输出有最大 value 的 key -def find_max(hash) +def find_max(h) # ... + h.values.max end h = { @@ -15,5 +16,3 @@ def find_max(hash) answer = find_max(h) puts "有最大 value 的是 #{answer}" # 应该是 d - - From 793e469c6efb50b0afbb3d481a068852e4118acc Mon Sep 17 00:00:00 2001 From: liyangbin Date: Wed, 30 Aug 2017 22:56:35 +0800 Subject: [PATCH 24/28] 24 --- 24-hash-even.rb | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/24-hash-even.rb b/24-hash-even.rb index 9da9605..7d29d18 100644 --- a/24-hash-even.rb +++ b/24-hash-even.rb @@ -1,9 +1,15 @@ # 给定一 Hash,输出 value 是偶数的 keys -def find_even_keys(hash) +def find_even_keys(h) # ... (请回传一个数组) - + new_array = [] + h.each do |key, value| + if value % 2 == 0 + new_array << key + end + end + new_array end h = { @@ -17,5 +23,3 @@ def find_even_keys(hash) answer = find_even_keys(h) puts "有偶数 value 的 keys 有 #{answer}" # 应该是数组 [b,d,e] - - From 6cb3b387a621aff024a17262163640cd98703d7e Mon Sep 17 00:00:00 2001 From: liyangbin Date: Thu, 31 Aug 2017 21:59:51 +0800 Subject: [PATCH 25/28] 26 --- 25-hash-count.rb | 4 +++- 26-hash-filter.rb | 18 +++++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/25-hash-count.rb b/25-hash-count.rb index 2167335..3b7e694 100644 --- a/25-hash-count.rb +++ b/25-hash-count.rb @@ -5,8 +5,11 @@ def count(arr) arr.each do |i| # ... + frenquency = arr.count(i) + h[i] = frenquency end + return h # 回传一个 hash end @@ -15,4 +18,3 @@ def count(arr) answer = count(arr) puts answer # 答案应该是 {"a"=>3, "d"=>6, "c"=>5, "b"=>1, "e"=>5} - diff --git a/26-hash-filter.rb b/26-hash-filter.rb index 51ade64..5563559 100644 --- a/26-hash-filter.rb +++ b/26-hash-filter.rb @@ -10,7 +10,23 @@ # .... -puts "所有成年人,并由小到大: _________" + +# def filter_array(arr) +# result_arr = [] +# arr.each do |h| +# if h["age"] >= 18 +# result_arr.push(h) +# end +# end +# result_arr.sort_by { |i| i["age"]} +# end + +def filter_array(arr) + arr.sort_by { |i| i["age"]}.select { |a| a["age"] >= 18} +end + + +puts "所有成年人,并由小到大: #{filter_array(arr)}" # 答案应该是 #[ From 87270d5af489be93124e25bc55d8dd9523840006 Mon Sep 17 00:00:00 2001 From: liyangbin Date: Thu, 31 Aug 2017 22:03:45 +0800 Subject: [PATCH 26/28] 27 --- 27-class.rb | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/27-class.rb b/27-class.rb index 8cec2c9..4ba539c 100644 --- a/27-class.rb +++ b/27-class.rb @@ -1,5 +1,12 @@ class Person # ... + attr_accessor :first_name + attr_accessor :last_name + + def greet + puts "Hello, #{@first_name} #{@last_name}" + end + end p1 = Person.new @@ -11,6 +18,3 @@ class Person p2.first_name = "William" p2.last_name = "Zhang" p2.greet # 输出 "Hello, William Zhang" - - - From 76fafb5308adef1022bac20ce88660b134bcc823 Mon Sep 17 00:00:00 2001 From: liyangbin Date: Thu, 31 Aug 2017 22:08:03 +0800 Subject: [PATCH 27/28] 28 --- 28-word-count.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/28-word-count.rb b/28-word-count.rb index 2643123..496c519 100644 --- a/28-word-count.rb +++ b/28-word-count.rb @@ -2,4 +2,13 @@ doc = File.read("wordcount.txt") +arr = doc.split ("") +h = {} +arr.uniq.each do |i| + h[i] = arr.count(i) +end +puts h + + + # ... From 9acb6e8cd19d5e1a415b1b59841cd38efb1a47a8 Mon Sep 17 00:00:00 2001 From: liyangbin Date: Thu, 31 Aug 2017 22:31:18 +0800 Subject: [PATCH 28/28] 29 --- 29-todos.rb | 17 ++++++++++++++++- dotos.txt | 5 +++++ 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 dotos.txt diff --git a/29-todos.rb b/29-todos.rb index 0bddde2..c4e12e3 100644 --- a/29-todos.rb +++ b/29-todos.rb @@ -15,19 +15,34 @@ print "请输入指令 1. add 2. remove 3. save,然后按 Enter: " command = gets.chomp + todo = nil if command == "add" print "请输入代办事项: " # ... + tode = gets.chomp + todos << todo elsif command == "remove" print "请输入要删除的编号: " # ... + remove = gets.chomp.to_i + todos.delete_at(remove) elsif command == "save" + File.open("dotos.txt", "w+") do |f| + todos.each do |i| + f << i + f << "\n" + end + end + puts "存盘离开" # ... + puts "现代办事项为:" + todos.each_with_index do |todo, index| + puts "#{index}: #{todo}" + end break; else puts "看不懂,请再输入一次" end end - diff --git a/dotos.txt b/dotos.txt new file mode 100644 index 0000000..fdca5f3 --- /dev/null +++ b/dotos.txt @@ -0,0 +1,5 @@ +Buy book +Go Shopping +Walk +Gogo +