循环范围和自定义输出

时间:2018-01-26 19:51:16

标签: ruby for-loop range

puts 'Please insert a number between 1 and 100.'
num = gets.to_i

if (0..100).step(0) do |n|
#     n = 10
#     n = 20
#     n = 30
#     ...
    puts 'Thanks. You inserted ' + num.to_s + '!'

#    # using case 
#    case num2
#    when 60
#      puts "Student gets F grade"
#    when 60..70
#      puts "Student gets D grade"    
#    when 70..80
#      puts "Student gets C grade"    
#    when 80..90
#      puts "Student gets B grade"    
#    when 90..100
#      puts "Student gets A grade"    
#    else
#      puts "Grade not available for that value."
#    end    

else n => (100)

    puts 'Please insert a number between 1 and 100.'
    num = gets.to_i
    puts 'Thanks. You inserted ' + num.to_s + '!'

end
  • 上面注释掉的代码是我试图消除的代码 在ruby中使用for else 循环完成。
  • 基本上我想检查用户是否输入了一个号码 有效范围(0 - 100),然后执行与之关联的代码,如果用户输入超出范围的数字,请使用else再次尝试。
  • 我想吐出一个与输入值相关的字母等级。 因此,如果输入有效值 - >获得与 - >相关的成绩否则' 请再试一次'。任何想法如何实现这一点与更多 最佳循环,而不是我讨厌的多个案例以前的代码?

更新

puts 'Please insert a number between 1 and 100.'
num = gets.to_i

while !num.between?(1, 100)
#num = gets.chomp.to_i

grade = loop do
  puts 'Check your score!'
  input = gets.to_i  
  case input
  when 1..60
    break 'F'
  when 60..70
    break 'D'
  when 70..80
    break 'C'      
  when 80..90
    break 'B'
  when 90..100
    break 'A'              
  else
    puts 'Please insert a VALID number between 1 and 100.'
    num = gets.to_i
  end      
end    

puts "Your grade is #{grade}"

end

4 个答案:

答案 0 :(得分:2)

我认为你对case-expression有正确的想法。只需将其全部放入loop并将其返回值分配给变量即可。在案例分支中使用break('grade')

grade = loop do
  puts 'What\'s your score'
  input = gets.to_i  
  case input
  when 1..10
    break 'F'
  when 11..20
    break 'E'
  else
    puts 'invalid score try again'
  end
end

puts "Your grade is #{grade}"

答案 1 :(得分:1)

这是以类似Ruby的方式获得所需结果的众多方法之一。

NumberToLetter = [['F', 59], ['D', 69], ['C', 79], ['B', 89], ['A', 100]]

def letter_grade
  num = number_grade
  letter, _ = NumberToLetter.find { |_, n| num <= n }
  [num, letter]
end

def number_grade
  loop do
    puts 'Please insert a number between 1 and 100.'
    num = gets.to_i
    break(num) if (1..100).cover?(num)
  end
end

10.times { print letter_grade; puts }
# [43, "F"]
# [59, "F"]
# [60, "D"]
# [69, "D"]
# [70, "C"]
# [79, "C"]
# [80, "B"]
# [89, "B"]
# [90, "A"]
# [100, "A"]

返回的每个双元素数组的第一个元素是用户输入的数字。第二个是相关的等级。

我采用了Ruby约定,表示一个未使用下划线('_')计算的局部变量,实际上它是一个有效的变量名。

答案 2 :(得分:0)

你认为这更简单,如果现在我明白了,你只需要询问用户的输入是否在你想要的范围内,如果是你跳过这段时间并做你想做的事情:

完整的代码是:

puts 'Please insert a number between 1 and 100.'
num = gets.to_i

while not num.between?(0,100) do

    puts 'Please insert a number between 1 and 100.'
    num = gets.to_i

end

puts 'Thanks. You inserted ' + num.to_s + '!'
case num
  when 60
    puts "Student gets F grade"
  when 60..70
    puts "Student gets D grade"    
  when 70..80
    puts "Student gets C grade"    
  when 80..90
    puts "Student gets B grade"    
  when 90..100
    puts "Student gets A grade"    
  else
    puts "Grade not available for that value."
end    

答案 3 :(得分:0)

你过度设计了这个。

num = 0 

while !num.between?(1, 100)
  puts 'Please insert a number between 1 and 100.'
  num = gets.chomp.to_i
end

puts "Thanks, #{num}"