2013-04-08 83 views
-2
  1. 我正在製作一個軟件,但我現在不想發佈它的源代碼,因爲我不想讓人們偷走我的辛勤工作。我不是粗魯的或任何類似的東西。下面是我製作的程序的一個例子。
print "Username : " 
name = gets.chomp 


print "Password : " 
pass = gets.chomp 

if name =="user" and pass=="pa$$word" 
print "Hello" 

else print "Error, incorrect details" 

現在,這是在Ruby中最簡單登錄形式,但什麼不好的事情發生在這裏,只要用戶插入錯誤的信息的程序將簡單地關閉,我想發生什麼事是,我想讓程序詢問用戶正確的信息,直到插入正確的信息。我需要紅寶石幫助

  1. 你是一個Windows用戶嗎?知道如何在批處理文件中編程? 例子

    回聲你好世界 CLS

暫停

所以這裏是Ruby

a. print "Command : " 
b. command = gets.chomp 

c. if command == "Hello" 

d. print "Hi, how are you?" 

e. elsif command == "help" 

f. print "Say hi to me, chat with me" 
現在

的代碼是什麼我也想在這裏就像在第一個問題

詳細說明:使用後r類型在「嗨」程序關閉,但我想在這裏它讓程序要求再去一行

+0

需要一個更具描述性的標題。 – 2013-04-08 15:30:14

+0

感謝所有回答我的問題的人。最後,我自己找到了解決方案:D – 2013-04-12 10:27:56

+0

您可以回答自己的問題,以便人們可以對其進行投票。 – 2013-04-12 12:52:07

回答

0

這裏是容易的方法:)如果有人卡住了我遇到的所有問題就是這個。 「while」循環

number = 1   # Value we will give for a string named "number" 
while number < 10 # The program will create a loop until the value 
        # of the string named "number" will be greater than 10 

    puts "hello" 

        # So what we need to do now is to end the loop otherwise 
        # it will continue on forever 
        # So how do we do it? 
        # We will make the ruby run a script that will increase 
        # the string named "number"'s value every time we run the loop 

    number = number + 1 
end     # Now what happens is every time we run the code all the program 
        # will do is print "hello" and add number 1 to the string "number". 
        # It will continue to print out "hello" until the string is 
        # greater than 10 
0

使用while循環不斷要求輸入並檢查用戶的提交,直到有效的結果是進入。

username = nil 

while username.nil? 
    puts "What is your username?" 
    entered_username = gets.chomp 

    if entered_username == "Bob" 
    username = entered_username 
    end 
end 

puts "Thanks!" 

當終端上運行,這產生:

What is your username? 
sdf 
What is your username? 
dsfsd 
What is your username? 
sdfsd 
What is your username? 
sdfds 
What is your username? 
sdf 
What is your username? 
Bob 
Thanks! 
0

1.

until (print "Username : "; gets.chomp == "user") and 
     (print "Password : "; gets.chomp == "pa$$word") 
    puts "Error, incorrect details" 
end 
puts "Hello" 

2.

loop do 
    print "Command : " 
    case gets.chomp 
    when "Hello" then print "Hi, how are you?"; break 
    when "help" then print "Say hi to me, chat with me"; break 
    end 
end