2013-04-08 122 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