2016-03-06 46 views
0
print"Do you want to calculate your invoice? Please enter Yes(Y) or No(N)... " 
calculate_cont = "" 
while calculate_cont = gets.chomp() 
    calculate_cont.to_s 
    calculate_cont.upcase! 
    case calculate_cont 
    when "Y", "YES" 
     <calculations> 
     print "Do you wish to continue? " 
    when "N", "NO" 
     puts "Thank you! Press Enter to exit!" 
     gets() 
     exit() 
    else 
    puts "Error: #{calculate_cont} is not a valid entry! 
    puts "Please Enter a valid response! Yes(Y) or No(N)" 
    print "Do you wish to continue? " 
    end 
end 

當用戶輸入除YES或NO之外的其他內容時,它將顯示他使用#{calculate_cont}輸入的文本。我如何告訴用戶,如果用戶只需使用#{calculate_cont}進行輸入,他就會輸入「Nothing」?可以做到嗎?ruby​​ show#{}當沒有輸入任何內容時

感謝

回答

0

是的,如果你在呼喚gets.chomp和用戶輸入任何值時,輸出將是一個空字符串。只要把另一個條件

DO SOMETHING if calculate_cont.empty? 
0

我創造了這個方法:

方法

def output(calculate_cont) 
    if calculate_cont == "" 
     return "Nothing" 
    else 
     return calculate_cont 
    end 
end 

與此輸出..

print"Do you want to calculate your invoice? Please enter Yes(Y) or No(N)... " 
calculate_cont = "" 
while calculate_cont = gets.chomp() 
    calculate_cont.to_s 
    calculate_cont.upcase! 
    case calculate_cont 
    when "Y", "YES" 
     <calculations> 
     print "Do you wish to continue? " 
    when "N", "NO" 
     puts "Thank you! Press Enter to exit!" 
     gets() 
     exit() 
    else  
    system("cls") 
    puts "" 
    puts "Error: #{output(calculate_cont)} is not a valid entry!" 
    puts "Please Enter a valid response! Yes(Y) or No(N)" 
    print "Do you wish to continue? " 
    end 
end 

這樣我可以告訴他輸入的用戶。 :)非常感謝

相關問題