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

时间:2017-12-06 15:49:42

标签: 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

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

但当然只返回文件中的整个列表。

基本上,我希望输出看起来像这样:

    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

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

1 个答案:

答案 0 :(得分:1)

在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 }

class Question
    attr_reader :choices, :text

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

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

@questions = []

IO.foreach('input.txt') do | line |
    code, text, validity = line.chomp.split(';')

    case code
    when 'Q'
        @question  = Question.new(text)
        @questions << @question
    when 'A'
        @question.add_answer(text, validity.strip == '1' ? true : false)
    else
        puts "Wrong code #{code}"
    end
end

p @questions

@questions.each do | question |
    answer       = nil
    valid_number = question.choices.index{ | item | item.last } # find the item [x, true]

    until answer == valid_number
        puts
        puts question.text
        question.choices.each_with_index do | choice, i |
            puts "#{i + 1}. #{choice.first}"
        end

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

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