Puts "Please input Number1:" #提示输入第一个数number1 =gets# Save the first number puts "please input a operator (+,-, *,/):" Op=getsputs "in Put Number2: "Number2=getscase op.chomp# performs the corresponding operation according to the operator when" + "Result=number1.to_i+number2.to_iwhen"-"result= Number1.to_i-number2.to_iwhen "*" Result=number1.to_i*number2.to_iwhen "/" result=number1.to_i/number2.to_iend# Output calculation result puts "Result:#{number1.chomp}#{op.chomp}#{number2.chomp}=#{result}"
Operation Result:
G:\share\ruby>ruby Number_guess.ry
Please enter a number between 0-100 to guess the size of the number randomly generated by the program
50
The number you entered is too small.
Please enter a number between 0-100 to guess the size of the number randomly generated by the program
75
The number you entered is too small.
Please enter a number between 0-100 to guess the size of the number randomly generated by the program
82
The number you entered is too small.
Please enter a number between 0-100 to guess the size of the number randomly generated by the program
91
The number you entered is too small.
Please enter a number between 0-100 to guess the size of the number randomly generated by the program
96
The number you entered is too small.
Please enter a number between 0-100 to guess the size of the number randomly generated by the program
98
The number you typed is too big.
Please enter a number between 0-100 to guess the size of the number randomly generated by the program
97
Congratulations, you guessed right.
Count=7
G:\share\ruby>
The ruby version of the guessing numbers game