diff --git a/01-hello.rb b/01-hello.rb index e0e7bbf..e4e56e9 100644 --- a/01-hello.rb +++ b/01-hello.rb @@ -5,4 +5,4 @@ # ... -puts "(请替换成最后的答案)" \ No newline at end of file +puts your_name diff --git a/02-variable.rb b/02-variable.rb index a5a4753..2821710 100644 --- a/02-variable.rb +++ b/02-variable.rb @@ -8,6 +8,5 @@ # ... -puts "a 应该是 2,现在是 #{a}" -puts "b 应该是 1,现在是 #{b}" - +puts "a 应该是 2,现在是 #{b}" +puts "b 应该是 1,现在是 #{a}" diff --git a/03-triangle.rb b/03-triangle.rb index fafec03..d4ed7bb 100644 --- a/03-triangle.rb +++ b/03-triangle.rb @@ -5,7 +5,7 @@ print "请输入直角三角形的底边,然后按 Enter: " b = gets - +t = (a.to_i * b.to_i)/2 # ..... -puts "直角三角形的面积是: _________" \ No newline at end of file +puts "直角三角形的面积是:#{t}" diff --git a/04-pizzas.rb b/04-pizzas.rb index 4c2521f..8eff592 100644 --- a/04-pizzas.rb +++ b/04-pizzas.rb @@ -7,6 +7,7 @@ people = gets # ..... - -puts "每人可分得几片: _________ 片" -puts "还剩下几片: _________ 片" \ No newline at end of file +t1= pizzas.to_i / people.to_i +t2= pizzas.to_i % people.to_i +puts "每人可分得几片: #{t1}片" +puts "还剩下几片: #{t2} 片" diff --git a/05-bmi.rb b/05-bmi.rb index 67efdff..cd2e5cc 100644 --- a/05-bmi.rb +++ b/05-bmi.rb @@ -11,7 +11,18 @@ height = gets # ..... +bmi = weight.to_f / (height.to_f * height.to_f) -puts "您的 BMI 是: _________" -puts "您的 BMI 结果是: _________(过轻或正常或过重)" \ No newline at end of file + +puts "您的 BMI 是: #{bmi}" + +if bmi<18.5 + r = "过轻" + elsif bmi>=24 + r = "过重" + else + r = "正常" +end + +puts "您的 BMI 结果是: #{r}" diff --git a/06-interger-positive.rb b/06-interger-positive.rb index a240f5f..2b1efe3 100644 --- a/06-interger-positive.rb +++ b/06-interger-positive.rb @@ -3,8 +3,22 @@ print "请输入一个整数,然后按 Enter: " x = gets - +t = x.to_i # .... -puts "这个数是_____ (正数或零或负数)" -puts "这个数是_____ (偶数或奇数)" \ No newline at end of file +if t > 0 + s = "正数" + elsif t < 0 + s = "负数" + else + s = "0" +end + +if t%2 == 0 + y = "偶数" +else + y = "奇数" +end + +puts "这个数是#{s}" +puts "这个数是#{y}" diff --git a/07-abcde.rb b/07-abcde.rb index 5d0c8c3..22f0805 100644 --- a/07-abcde.rb +++ b/07-abcde.rb @@ -18,5 +18,22 @@ z = gets # .... +x1 = x.to_i +x2 = y.to_i +x3 = z.to_i -puts "结果是________(A或B或C或D或E)" \ No newline at end of file +if x1 < 0 + r = "A" +elsif x1 >0 && x2 >0 && x3 >0 + r = "B" +elsif x1 >0 && x2 >0 && x3 <0 + r = "C" +elsif x2<0 && x3 >0 + r = "D" +elsif x2<0 && x3<0 + r = "E" +end + + + +puts "结果是#{r}" diff --git a/08-find-max.rb b/08-find-max.rb index 9e6e643..988fd40 100644 --- a/08-find-max.rb +++ b/08-find-max.rb @@ -11,4 +11,17 @@ # .... -puts "最大的数是 ________(x或y或z)" \ No newline at end of file +x1 = x.to_i +y1 = y.to_i +z1 = z.to_i +if x1 >= y1 && x1 >= z1 + r= x1 +elsif y1 >= x1 && y1>= z1 + r= y1 + +else + r= z1 +end + + +puts "最大的数是#{r}" diff --git a/09-function.rb b/09-function.rb index b1f922d..0e03d84 100644 --- a/09-function.rb +++ b/09-function.rb @@ -1,15 +1,15 @@ # 题目: 输入直角三角形的宽和高,输出三角形的面积 def calculate_area(a, b) - # .... + (b*a)/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 + answer = calculate_area(a,b) +puts "直角三角形的面积是: #{answer}" diff --git a/10-function.rb b/10-function.rb index bb450fb..4ca8d78 100644 --- a/10-function.rb +++ b/10-function.rb @@ -1,19 +1,29 @@ # 题目: 使用者输入 x,y,z,请输出三个数中最大的数 def find_max(x, y, z) + if x >= y && x >= z + r= x + elsif y>= x && y>= z + r= y + + else + r= z + end end print "请输入一个数字x,然后按 Enter: " -x = gets +x = gets().to_i print "请输入一个数字y,然后按 Enter: " -y = gets +y = gets().to_i print "请输入一个数字z,然后按 Enter: " -z = gets +z = gets().to_i # .... + + 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..44a2257 100644 --- a/11-seven.rb +++ b/11-seven.rb @@ -2,8 +2,9 @@ i = 1 while ( i <= 100 ) + if i%7==0 + puts "#{i}" - # .... - + end i+=1 -end \ No newline at end of file +end diff --git a/12-sum-even.rb b/12-sum-even.rb index 73879bb..56eb4cd 100644 --- a/12-sum-even.rb +++ b/12-sum-even.rb @@ -5,9 +5,10 @@ while ( i <= 100 ) - # .... - - i+=1 + 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..465daf6 100644 --- a/13-nn.rb +++ b/13-nn.rb @@ -1,11 +1,13 @@ # 题目: 输入一个数字 N,输出 N * N 乘法表 print "请输入数字 N,然后按 Enter: " -n = gets - -# while ( ... ) -# while ( ...) -# -# end -# end - +n = gets.to_i +i = 1 + 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..6deff02 100644 --- a/14-prime.rb +++ b/14-prime.rb @@ -1,13 +1,20 @@ # 输入一个数字 N,请检查是不是质数 def is_prime(n) -# .... + i = 2 + while i <= (n/2) + while n % i == 0 + return false + end + i+=1 + end + return true end print "请输入数字 N,然后按 Enter: " -n = gets +n = gets.to_i -if is_prime(n.to_i) +if is_prime(n) puts "这是质数" else puts "这不是质数" diff --git a/15-guess-number.rb b/15-guess-number.rb index 48f9dca..a64986d 100644 --- a/15-guess-number.rb +++ b/15-guess-number.rb @@ -5,13 +5,14 @@ while (true) 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 diff --git a/16-array-sum.rb b/16-array-sum.rb index 9b4910b..7b6fd47 100644 --- a/16-array-sum.rb +++ b/16-array-sum.rb @@ -1,11 +1,10 @@ # 给定一阵列内含数字,输出最大值 def find_max(array) - #.... + array.max() end -arr = [8, 12, 36, 53, 9, 75, 3, 71, 59, 88] +arr = [8, 12, 36, 53, 9, 158, 3, 71, 59, 88] max = find_max(arr) puts "Max is #{max}" # 应该是 88 - diff --git a/17-array-stats.rb b/17-array-stats.rb index 0af81bb..574dbd7 100644 --- a/17-array-stats.rb +++ b/17-array-stats.rb @@ -12,9 +12,28 @@ end end -puts arr.to_s +max = arr[0] +min = arr[0] +sum = 0 + +arr.each do |i| + if max < i + max = i + end + + if min > i + min = i + end + + sum = sum + i +end -puts "总和是 _____" -puts "平均是 _____" -puts "最大值是 _____" -puts "最小值是 _____" \ No newline at end of file + + + + +puts arr.to_s +puts "总和是 #{sum}" +puts "平均是 #{sum/arr.size}" +puts "最大值是 #{max}" +puts "最小值是#{min}" diff --git a/18-square.rb b/18-square.rb index 226e1c1..b9fda86 100644 --- a/18-square.rb +++ b/18-square.rb @@ -3,8 +3,12 @@ arr = [] print "请输入数字 N,然后按 Enter: " -n = gets - +n = gets.to_i +i = 1 # ... +while i<=n + arr<<(i-1)*(i-1) + i+=1 -puts arr.to_s \ No newline at end of file +end +puts arr.to_s diff --git a/19-filter.rb b/19-filter.rb index ef7e515..c4c69bf 100644 --- a/19-filter.rb +++ b/19-filter.rb @@ -1,9 +1,15 @@ # 给定一阵列内含数字,输出另一个数组只包含偶数 def filter_even(arr) - #... + array = [] + arr.each do |i| + if i % 2 == 0 + array<3, "d"=>6, "c"=>5, "b"=>1, "e"=>5} - diff --git a/26-hash-filter.rb b/26-hash-filter.rb index 51ade64..3a43f46 100644 --- a/26-hash-filter.rb +++ b/26-hash-filter.rb @@ -7,10 +7,17 @@ { "name" => "Steven", "age" => 22 }, { "name" => "Vincent", "age" => 6 }, ] +def filter(arr) + result_arr = [] + arr.each do |h| + if h["age"]>=18 + result_arr.push(h) + end + end + result_arr.sort_by{|i| i["age"]} -# .... - -puts "所有成年人,并由小到大: _________" +end +puts "所有成年人,并由小到大:#{filter(arr)} " # 答案应该是 #[ diff --git a/27-class.rb b/27-class.rb index 8cec2c9..a3f690e 100644 --- a/27-class.rb +++ b/27-class.rb @@ -1,5 +1,11 @@ class Person - # ... + attr_accessor :first_name + attr_accessor :last_name + + def greet + puts "Hi, #{@first_name}#{@last_name} " + + end end p1 = Person.new @@ -11,6 +17,3 @@ class Person p2.first_name = "William" p2.last_name = "Zhang" p2.greet # 输出 "Hello, William Zhang" - - - diff --git a/28-word-count.rb b/28-word-count.rb index 2643123..652dc59 100644 --- a/28-word-count.rb +++ b/28-word-count.rb @@ -2,4 +2,10 @@ doc = File.read("wordcount.txt") -# ... +words = doc.scan(/\w+/) +result = {} +words.each do |i| + result[i] = words.count(i) + +end +puts result diff --git a/29-todos.rb b/29-todos.rb index 0bddde2..4b266e4 100644 --- a/29-todos.rb +++ b/29-todos.rb @@ -17,17 +17,23 @@ if command == "add" print "请输入代办事项: " - # ... + add = gets + todos << add elsif command == "remove" print "请输入要删除的编号: " - # ... + remove = gets + todos.delete_at(remove.to_i) elsif command == "save" puts "存盘离开" + target = File.open("todos.txt", "w+") + for i in todos + target.write(i+"\n") + + end + target.close() - # ... break; else puts "看不懂,请再输入一次" end end - diff --git a/todos.txt b/todos.txt index 4757e85..59b74e5 100644 --- a/todos.txt +++ b/todos.txt @@ -1,4 +1,5 @@ Buy book Go Shopping -Walk Gogo +To cut hair +