好吧,我已尽力让这段代码在没有吐出错误的情况下运行,但无济于事。希望你能帮助我。
require 'launchy'
#def linebreak(breakline)
def program()
puts "Welcome to test program v1. Would you like to continue? ENTER y for Yes or n for no"
user_input_1 = gets.chomp
if user_input_1 == "y"
puts "How would you like to proceed CRASH | TEXTMAKER | UNDECIDED // CASE SENSITIVE"
user_input_2 = gets.chomp
if user_input_2 == "CRASH"
while true Launchy.open("http://google.com")
elsif user_input_2 = "TEXTMAKER"
while true out_file.puts("test program v1")
else
puts "You have not entered a method."
elsif user_input_1 == "n"
abort
else
puts "That is not a valid command. Please run the script again."
end
end
答案 0 :(得分:0)
好吧,有一些问题,但不要担心一切都可以 固定!
让我们从你做得很好
开始在大多数情况下使用你的布尔值很好,大多数初学者似乎都没有
掌握==
意味着平等,=
完全意味着什么
不同
puts
干得好,使用得当,还有其他
我将在稍后介绍的方法,在你的方面会更好看
情况下。
现在让我们来讨论可修复的内容
如上所述,在大多数情况下,您正确使用了布尔值
但是你似乎错过了一个。 elsif user_input_2 = "TEXTMAKER"
您需要==
来证明它是平等的。
您的while
循环似乎没有任何意义,因为您并未真正将true
设置为任何内容。在Ruby中它是高度的
建议永远不要使用while
循环,当然有时候
你必须这样做,但只有在你尝试了其他一切之后。
elsif; else; elsif
从不在elsif
之后使用else
。 elsif
是提供例外情况,在您的情况下会有更多例外
一个选项,所以在其他地方之前使用elsif
,你最好在这里使用一个案例陈述。user_input_2.gets.chomp.upcase
这将设置它,以便无论他们如何输入文本,它始终都是大写。end
一旦您使用了适当的缩进,这一点就会变得清晰好吧,让我们改写这件事:
require 'launchy'
def program #<= you don't need the parentheses
print "Welcome to the test program version 1, to continue type 'c' to exit type 'e': "
user_input = gets.chomp.upcase
if user_input == "C"
program_choices
elsif user_input == "E"
puts "Exiting.."
exit
#if you really want to use abort
#abort("Exiting..")
#you won't need to use exit if you use abort
else
puts "Invalid input, try again"
program #<= this is a loop known as recursion, don't use it to much
end
end
def program_choices
print "Pick an option, the choices include CRASH | TEXTMAKER | UNDECIDED: "
user_input = gets.chomp.upcase
case user_input #<= this is how you start a case statement
when "CRASH"
Launchy.open("http://google.com")
when "TEXTMAKER"
write_test_data
when "UNDECIDED"
puts "You really need to make up your mind.." #<= have some fun with it, that's the point of programming.
program_choices
else
puts "Invalid input, try again"
program_choices
end
end
def write_test_data
x = "Test file version 1"
if File.exist?("path/to/test/file.txt")
File.open("path/to/test/file.txt", "a"){ |s| s.write(x) }
else
new_test_file = File.new("path/to/test/file.txt")
new_test_file.puts(x)
end
end
和bam!您的程序已启动并运行!如果您对任何不要犹豫的问题有任何疑问,编程可能会非常棘手,而且可能非常困难。坚持下去,你就到了那里!
答案 1 :(得分:-1)
我看到很多问题。
这不是有效的:
while true Launchy.open("http://google.com")
这两个都不是:
while true out_file.puts("test program v1")
但是,我无法告诉你如何解决这个问题,因为它并不清楚你正在尝试做什么。 if
区块可能只有一个else
,并且必须在所有elsif
之后。
下面:
elsif user_input_2 = "TEXTMAKER"
您正在为user_input_2
分配新值。我猜你是想使用等于运算符,==
。
您的def
区块没有end
。在我编辑代码以使用适当的缩进后,这变得很明显。通过使用合理的缩进和空格,你可以省去很多麻烦。