Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby语法错误,意外的$end_Ruby_If Statement - Fatal编程技术网

Ruby语法错误,意外的$end

Ruby语法错误,意外的$end,ruby,if-statement,Ruby,If Statement,从昨天开始,我就一直在用一些Ruby在CodeCademy上玩,我觉得自己好像稍微掌握了一些,但有几个问题变得显而易见 这个代码有什么问题?我一直收到一个错误: (ruby):15:语法错误,意外$end,应为关键字\u end 有两个if语句没有相应的end if grade > 1 puts "You're just making that up! Really?" end 及 另一方面,我认为您需要的是put而不是print 此外,当您有一个布尔值时,您不需要这样做 if my

从昨天开始,我就一直在用一些Ruby在CodeCademy上玩,我觉得自己好像稍微掌握了一些,但有几个问题变得显而易见

这个代码有什么问题?我一直收到一个错误:

(ruby):15:语法错误,意外$end,应为关键字\u end


有两个
if
语句没有相应的
end

if grade > 1
  puts "You're just making that up! Really?"
end

另一方面,我认为您需要的是
put
而不是
print

此外,当您有一个布尔值时,您不需要这样做

if my_boolean == true
  # do something
end
这和做同样的事情

if my_boolean
  # do something
end

最后,布尔值只有两个可能的值,
true
false
。最后的
else
语句没有意义,因为只要
answer==true
,它就会被激活,而您已经有了这样的理由。

通过适当的缩进,它就会变得清晰

print "What grade did you get on your final?"
grade = gets.chomp
if grade > 1
  puts "You're just making that up! Really?"
  answer = gets.chomp
  yes = true
  no = false

  if answer == true
    print "Oh, that's awesome!"
    if answer == false
      print "Dang, I thought you were serious."
    else
      print "What? I don't understand."
    end

Ruby中的每个
if
都需要相应的
end
。您有三个
if
s,只有一个
end
。因此,只有最后一个
if answer==false
有一个匹配的
end
,其他的则需要两个。

您需要每个
if
都有一个end。 您可以这样做:

puts "What grade did you get on your final?"
grade = gets.chomp

if grade > 1
  puts "You're just making that up! Really?"
end

answer = gets.chomp

if answer == "yes"
  print "Oh, that's awesome!"
elsif answer == "no"
  print "Dang, I thought you were serious."
else
  print "What? I don't understand."
end
或者可以是这样,您不需要使用
end

puts "What grade did you get on your final?"
grade = gets.chomp

# HERE
puts "You're just making that up! Really?" if grade > 1

answer = gets.chomp

if answer == "yes"
  print "Oh, that's awesome!"
elsif answer == "no"
  print "Dang, I thought you were serious."
else
  print "What? I don't understand."
end

欢迎使用Stack Overflow,请确保您访问了该页面和该页面。我是这样做的:(粘贴代码)但仍然不确定如何使else语句工作的。我希望它说“什么?我不明白。”如果用户输入的不是“是”或“否”。我考虑了| |函数,但不确定该函数如何正常工作。您希望与用户所说的进行比较
answer==“yes”
然后你可以做一个
els如果answer==“no”
,那么你可以使用你的
else
语句。
puts "What grade did you get on your final?"
grade = gets.chomp

if grade > 1
  puts "You're just making that up! Really?"
end

answer = gets.chomp

if answer == "yes"
  print "Oh, that's awesome!"
elsif answer == "no"
  print "Dang, I thought you were serious."
else
  print "What? I don't understand."
end
puts "What grade did you get on your final?"
grade = gets.chomp

# HERE
puts "You're just making that up! Really?" if grade > 1

answer = gets.chomp

if answer == "yes"
  print "Oh, that's awesome!"
elsif answer == "no"
  print "Dang, I thought you were serious."
else
  print "What? I don't understand."
end