Ruby如何重新启动程序并创建一个类

时间:2014-04-13 02:58:31

标签: ruby

我在ruby中制作一个简单的测验程序,但我需要帮助来回答测验问题 我对这整个红宝石的事情非常糟糕,但我会尝试解释,我希望程序在答案不是A,B,C或D时循环,但我也希望程序能够能够不在乎答案是A还是A或B或b以及其他什么,我已经得到常数A的语法错误是未定义的和意外的结局和东西。

require 'colorize'
require 'win32console'

puts "Hello! And Welcome the game that you shall name!".yellow  
puts "My name is Wuz and I shall be your guide for the time being!".yellow
print "Now young quester, what shall I call you?".yellow
call_you = gets.chomp
puts "Okay quester #{call_you} now we shall name the game!".yellow
print "Okay #{call_you} what shall the game be named?".yellow
named = gets.chomp
puts "#{call_you}! this game shall be called #{named}!".yellow
puts "I Will give you some instructions to begin the game and then I will be off as I have business to attend to in other parts of the internet!".yellow
puts "This is a simple text quiz game which will test your wits and other things!"
puts "To answer the questions in green all you must do is either input A,B,C or D or enter the correct word/number"
print "Ok?"
puts "Lets do a trial question!"

puts "What color is an orange?"

puts "A)Black"
puts "B)Red"
puts "C)Orange"
puts "D)Every colour!"
print "What shall you say?".red

say = gets
if
  say = A
  put "Wrong!".red
elsif
  say = B
  put "Wrong!".red
elsif
  say = C
  put "Right! Good job!".green
elsif
  say = D
  put "Wrong!".red
end

2 个答案:

答案 0 :(得分:1)

如果要包含大量文本,可以考虑添加更大的块 在文本文件或Here documents中。这是后者的一个例子:

GREETING =<<_
puts "Hello! And Welcome the game that you shall name!".yellow  
puts "My name is Wuz and I shall be your guide for the time being!".yellow
_

INSTRUCTIONS =<<THE_END
I Will give you some instructions to begin the game and then I will be off as
I have business to attend to in other parts of the internet!
This is a simple text quiz game which will test your wits and other things!
To answer the questions in green all you must do is either input A,B,C or D
or enter the correct word/number.

Ok?
Let's do a trial question!

What color is an orange?

A) Black
B) Red
C) Orange
D) Every colour!
THE_END

我们现在可以在您的代码中使用这些常量:

puts GREETING

print "Now young quester, what shall I call you?"
call_you = gets.chomp

puts "Okay quester #{call_you} now we shall name the game!".
print "Okay #{call_you} what shall the game be named?"
named = gets.chomp

puts "#{call_you}! this game shall be called #{named}!"

puts INSTRUCTIONS

您可能希望在循环内提​​出问题,然后在给出有效答案时突破。在获得变量中的问题答案后,我使用case语句来处理响应。首先,我将响应转换为大写,以防输入小写字母。

如果答案为"C"(或"c"),则会打印"Right! Good job!"并退出循环。 如果答案是“A”,“B”,“D”,则打印"Wrong!"并且循环存在。如果给出任何其他答案,则重复循环。

say = nil
loop do
  print "What shall you say? "
  say = gets.strip
  case say.upcase
  when "C"
    puts "Right! Good job!"
    break
  when "A", "B", "D"
    puts "Wrong!"
    break
  else
    puts "Invalid entry"
  end
end
puts "#{say} was entered"

第一行say = nil的唯一原因是初始化变量say,以便在块结束后检索其值。 say可以初始化为任何值。如果省略此语句,则say的范围仅限于块。

答案 1 :(得分:0)

这里有很多问题。

if
say = A

指定变量A的值来表示并测试是否为真,而不是比较&#34; A&#34;的值。说。 做我认为你想做的事应该是

if say.chomp == "A"
  puts ....

另外请清理结构... puts"blah"应该放置&#34; blah&#34;等等。

<强>更新 解决评论中的问题

也许你可以把整个事情放在一个循环中:

wrong_answer = true

while wrong_answer do 
...
...
# for the correct response case
if say.chomp == "A" 
puts ....
wrong_answer = false