I'm new to Ruby, and am having issues getting it to do what I need. It is an easier language than C++ (when I tried to learn it), and I am slowly (but surely) catching onto it.
I am trying to get the average amount of rainfall for all months, the amount of months and average rainfall per month. I think I am using the wrong thing (attr_accesor) to get what I need, I might have the right idea but I'm not doing it the correct way.
The first loop I made collects the amount of years and iterates once. The nested loop asks the user for the amount of inches of rain per month and iterates 12 times. At least, I think that's what I'm telling it to do.
class Screen
def cls
puts ("\n" * 25)
end
end
class Amount
attr_accessor :year
attr_accessor :Jan, :Feb, :Mar, :Apr, :May, :Jun, :Jul, :Aug, :Sep, :Oct, :Nov, :Dec
end
Inches = Amount.new
puts "How many years to calculate?"
year = STDIN.gets
year.chop!
loop do
if year.to_i < 1
puts "Do not insert a number less than 1 year."
answer = STDIN.gets
break if year.to_i >= 1
end
#MONTHS
puts "Rainfall for January? "
Jan = STDIN.gets
The rest of the months follow using same syntax. The code continues with:
#Invalid number, negative number for monthly rainfall
if Inches.to_i <= 0
puts "Cannot insert a negative number for amount of rainfall"
break if Inches.to_i >= 1
end
end
I do get errors but they're something silly with the loop and math operators and I'm trying to work them out right now. I am pretty sure I am able to calculate the average and months at the end on my own, so I did not include it in the code.
Can anyone point out what part of the code I need to look into or suggest a better way?

New Topic/Question
Reply



MultiQuote










|