class Leap_year def initialize puts 'Please input a year:' end def leap_year?(year) @year = year if @year % 400 == 0 puts 'Leap year!' elsif @year % 4 == 0 && @year % 100 != 0 puts 'Leap year!' else puts 'Not a leap year' end end end
leap = Leap_year.new testyear=gets.chomp.to_i leap.leap_year?(testyear)
报错信息: leap_year.rb:18: private method `chomp' called for nil:NilClass (NoMethodError) Please input a year: 初步怀疑是这里的问题 testyear=gets.chomp.to_i,但是不明白是什么问题,请大牛们指点指点。