+ 1
how to declare a input number positive or negative?
If input>0 = positive, x<0 negative, x=0 is zero...... Help me down
6 Respostas
+ 2
There is nothing wrong with the comparison logic by itself. All that has to be changed is to compare x.to_i with 0 instead of just normal x since gets.chomp by default receives input as strings so you have to cast the string to an integer with the str.to_i method to make comparisons with an integer.
+ 1
Declaration doesn’t change for a positive or negative number, and yeah for “if” conditions, that is how you would check. Idk if that was what you were asking for. This question was kind of worded unclearly.
Are you learning Ruby because of Shopify too?
+ 1
Michael David
I am a new learner of Ruby programming.
x= gets.chomp
If x>0
Puts "positive"
else
Puts "negative"
end
Please correct my code...
+ 1
Here is the solution
x= gets.chomp
If x.to_i > 0
Puts "positive"
else
Puts "negative"
end
0
prashanth Run error 😢😢
0
I think it was indentation that was the problem.