Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.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
Arrays 使用数组在Ruby中构建多项选择题测验_Arrays_Ruby - Fatal编程技术网

Arrays 使用数组在Ruby中构建多项选择题测验

Arrays 使用数组在Ruby中构建多项选择题测验,arrays,ruby,Arrays,Ruby,我想创建一个Ruby程序,用户可以在其中进行多项选择测试 我有每个测试的文件,其中问题和答案存储如下: Q;Who is the President of the USA? A;Obama;0 A;Trump;1 A;Bush;0 Q;When did Iceland gain independence? A;1944;1 A;1901;0 A;1974;0 Who is the President of the USA? 1. Obama 2. Trump

我想创建一个Ruby程序,用户可以在其中进行多项选择测试

我有每个测试的文件,其中问题和答案存储如下:

Q;Who is the President of the USA?
A;Obama;0 
A;Trump;1 
A;Bush;0
Q;When did Iceland gain independence?
A;1944;1
A;1901;0
A;1974;0
    Who is the President of the USA?
    1. Obama
    2. Trump
    3. Bush
Please enter answer

    When did Iceland gain independence?
    1. 1944
    2. 1901
    3. 1974
Please enter answer
Q表示问题,a表示答案选项。 值1分配给正确答案,0分配给错误答案

我已将行转换为数组,如下所示:

IO.foreach("#{test_name}.txt") do |line|
  line.chomp! 
  exam = line.split(";") 
end
我的问题是我想显示一个问题 然后是答案选项,直到我进入下一个问题

我一直在IO中使用
puts
,如下所示:

IO.foreach("#{test_name}.txt") do |line|
  line.chomp!
  exam = line.split(";") 
puts exam[1] + "\n" 
  end
end
当然,这只是返回文件中的整个列表

基本上,我希望输出如下所示:

Q;Who is the President of the USA?
A;Obama;0 
A;Trump;1 
A;Bush;0
Q;When did Iceland gain independence?
A;1944;1
A;1901;0
A;1974;0
    Who is the President of the USA?
    1. Obama
    2. Trump
    3. Bush
Please enter answer

    When did Iceland gain independence?
    1. 1944
    2. 1901
    3. 1974
Please enter answer

基本上,我想知道是否有一种方法可以读取这样的文件,最好是对答案选项进行编号?

在Ruby中有很多方法。这是众多解决方案中的一个,快速制定,只是为了让您走上正确的轨道。它缺乏测试,例如验证行的内容是否符合预期

class Question
    def initialize(p_text)
        @choices = []
        @text    = p_text
    end

    def add_answer(p_choice, p_validity)
        @choices << [p_choice, p_validity]
    end

    def ask
        valid_number = nil
        puts
        puts @text
        @choices.each_with_index do | choice_and_validity, i |
            choice, validity = choice_and_validity
            valid_number     = i if validity
            puts "#{i + 1}. #{choice}"
        end

        print 'Please enter answer', ' > '
        answer = gets.chomp.to_i - 1

        unless answer == valid_number
            puts 'Wrong answer.'
            ask
        end
    end
end

@questions = []

IO.foreach('input.txt') do | line |
    case line[0]
    when 'Q'
        code, text = line.chomp.split(';')
        @question  = Question.new(text)
        @questions << @question
    when 'A'
        code, choice, validity = line.chomp.split(';')
        @question.add_answer(choice, validity.strip == '1' ? true : false)
    else
        puts "Wrong code #{line[0]}"
    end
end

p @questions

@questions.each{ | question | question.ask }
课堂提问
def初始化(p_文本)
@选项=[]
@text=p_text
结束
def添加应答(p_选择,p_有效性)

@在代码中需要更多的逻辑。Ruby没有魔法助手来帮助你。你可能想开始阅读。另一件需要注意的是,你的问题非常模糊,似乎你在向人们询问整个解决方案。谢谢你@hans,我试图尽可能清楚地回答这个问题,但我知道我可能会离开。我对Ruby和stack overflow非常陌生,但我一直在使用Ruby、stack overflow等书籍,试图找到自己的解决方法。但到目前为止,我还没有找到任何东西。我只是想知道是否有一种方法可以读取这样的文件/显示数组,或者我是否完全偏离了正轨。您必须使用Ruby对象为测验数据找到合适的表示形式。可以是自定义类,也可以是哈希和数组的组合。然后您可以读取文件并相应地创建/填充对象。最后你可以循环提问并收集答案。谢谢!这绝对让我走上了正确的道路!