2011-10-12 35 views
0

只是需要一點點拋光工作輸出..安排在不同的行

command = "" 
names = [] 
while command != "exit" 
puts 'please enter names seperated by a space: ' 

command = gets.chomp! 

if command == "quit" 
    names.sort! do|a,b| a.upcase <=> b.upcase end   # use {...} for do\end, in a single entry rather then multiple 
    names.each_with_index do |name, index| 
    if index % 2 == 0 
     puts "#{name}", "\n" 
     else 
     puts "#{name.reverse}", "\n" 
    end 
end 
    else 
    names << command 
    end 
end 

所以這是什麼編程'的作用是顯示一個數字,其中由用戶輸入的名字。我無法做到的是在對方下面顯示名字。例如。 搶 法案 露西 斬 ... 它然後向後顯示每秒名稱,在這樣的

法案

露西

印章

顯示它們

======

llib

pohc。

lolmy大腦煮熟有什麼建議?

回答

0

這裏是我會怎麼做:

names = [] 
while true 
    puts 'please enter names seperated by a space: ' 
    command = gets.chomp 
    break if command == 'exit' #exit loop if exit is entered 
    command.split(' ').each {|a| names.push(a)} # split up each word with space as delimiter and insert into the array 
end 


temp = [] 
names.each do |each| #cycle thru array 
    puts each # print each entry 
    temp.push(each) if names.index(each).odd? #if entry is odd then add to temp array 
end 
puts '======' 
temp.each {|each| puts each.reverse} #print each entry reversed 

我可以嘗試修復你的,如果你想...

+0

感謝的人真正幫助 –