V
V
Vladislav Bukach2020-07-02 21:52:01
ruby
Vladislav Bukach, 2020-07-02 21:52:01

I'm new to ruby ​​so don't be too harsh. Do I need help with arrays?

I want it to be removed from the array after each output of the question so that it would not be repeated during the further work of the program. Forgive me if I posted my thought a little confusingly.

arr = []
i = 0
b = gets.chomp.to_i

while i < b do
  i += 1
  n = gets.chomp
  arr << n
end


puts "Если вы готовы к проверки зачета нажмите 1 в противном случаи любое другое число"
r = gets.chomp.to_i
puts "Введите количество задоваемых вопросов"
g = gets.chomp.to_i
o = 0


while o < g do
  o += 1
  if r == 1
    puts arr.sample
    end
end

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
GeneAYak, 2020-07-03
@vlad12156

You presented, in principle, not the simplest and most convenient solution, it is much easier to sort the array in advance in random order, take the required number of questions and issue them until they run out

arr.shuffle.take(g).each { |question| puts question }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question