简单测验的输出问题

时间:2016-03-08 11:32:34

标签: ruby

在下面的代码和输出到控制台,似乎一切都很好,但最后我的输出格式看起来很糟糕;每次调用一个值时,它都会在它之后立即生成换行符。

代码:

puts "Please enter your name: "
name = gets
puts "Please enter your age: "
age = gets
puts "Please enter the name of your favourite movie: "
movie = gets
puts "Please rate this movie out of 10: "
movieR = gets

puts "Just to confirm; your name is #{name}.\nYou are #{age} years old.\nYour favourite movie is #{movie}, which you rate as #{movieR}/10"

控制台输出:

Please enter your name:                                                                                                                                                                 
Rob                                                                                                                                                                                     
Please enter your age:                                                                                                                                                                  
26                                                                                                                                                                                      
Please enter the name of your favourite movie:                                                                                                                                          
Jurassic Park                                                                                                                                                                           
Please rate this movie out of 10:                                                                                                                                                       
10                                                                                                                                                                                      
Just to confirm; your name is Rob                                                                                                                                                       
.                                                                                                                                                                                       
You are 26                                                                                                                                                                              
 years old.                                                                                                                                                                             
Your favourite movie is Jurassic Park                                                                                                                                                   
, which you rate as 10                                                                                                                                                                  
/10

1 个答案:

答案 0 :(得分:2)

这是由gets使用\ n(换行符)作为输入的默认分隔符引起的,除非提供了另一个(它不在此处),因此当您显示结果时,包含换行符分隔符作为价值的一部分;请改用gets.chomp