-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

暂停

所以这是红宝石的代码

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"

现在我想要的也和第一个问题一样

详细信息:在用户输入“嗨”后,程序就关闭了,但我想在这里让程序要求再次进入第 a 行

4

3 回答 3

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!
于 2013-04-08T15:34:36.170 回答
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
于 2013-04-08T15:36:40.283 回答
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
于 2013-04-12T23:05:27.753 回答