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中最简单的登录表单,但是这里发生的不好的事情是,只要用户插入错误的信息,程序就会关闭,我想要发生的是我想让程序向用户询问正确的信息,直到插入了正确的信息。
您是Windows用户吗?知道如何在批处理文件中编程吗? 例子
echo Hello world 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"
现在我想要的就像第一个问题一样
详细信息:用户输入"嗨"该程序刚关闭,但我想在这里让程序要求再次排队
答案 0 :(得分: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!
答案 1 :(得分: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
答案 2 :(得分: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