Wednesday, April 11, 2012

ruby regular expressions

I am trying to create a program where a user enters four numbers using regular expressions . If one of those numbers is 13 then the numbers to the left do not count toward the sum. My problem is creating an exception where none of the numbers equal 13. I cant seem to find a regular expression for my exception
thanks in advanced



puts "enter a number then hit enter four times"

number1 = STDIN.gets

number2 = STDIN.gets
number3 = STDIN.gets

number4 = STDIN.gets


if number1 =~ /13/ then
puts number2.to_i + number3.to_i + number4.to_i
end

if number2 =~/13/ then
puts number3.to_i + number4.to_i
end

if number3 =~/13/ then
puts number4.to_i
if number4 =~/13/ then
puts "0"
end
if number1 != 13 or number2 != 13 or number3 != 13 or number4 != 13
puts number1.to_i + number2.to_i + number3.to_i + number4.to_i
end
end




No comments:

Post a Comment