I would like to create a program that inputs numbers from the terminal and changes the processing according to the numbers.
If it is +10 or less, it is "a number of 10 or less"
Create a program that can output like this.
First, enter a number from the terminal and assign it to a variable.
input = gets.to_i
All the values entered using the gets method are strings, so even if you enter numbers, they will be converted to strings when used in the program. Therefore, I use the to_i method to convert it to a number.
Next, we will make a conditional branch using the if statement. Since the if statement makes a judgment based on the conditions written earlier, it will be passed through even if the conditions written below it are met.
We will consider the order with this rule in mind.
input = gets.to_i
if input <= 0
puts "It is a number less than 0"
elsif input <= 10
puts "Is a number less than 10"
else
puts "Is a number greater than 10"
end
By writing in this order, even if a number of 0 or less is input, it will not be output if the number is 10 or less.
Recommended Posts