0

我在运行以下内容时遇到了困难。我在

puts shopper[i].name

线

好像有时对象到达那条线时还没有创建

(0 .. 9).each do |i|
  shopper[i] = Shopper.new("Shopper Number-#{i}")
  (0 .. 19).each do |j|
    r = rand(0 .. (k-1))
    if shopper[i].unsuccessful_shopping_trip == true
      puts "#{shopper[i].name} has failed to buy 3 times and has decided to go home"
      i += 1
      break
    else
      if shopper[i].add_product(shop.remove_product_bought_by_shopper(r)) == false
        puts "#{shopper[i].name} has tried to buy #{(shop.products[r]).name} but it is sold out"
        j -= 1
      else
        puts "#{shopper[i].name} has bought #{(shop.products[r]).name}"
      end
    end
  end

  puts shopper[i].name
  puts shopper[i].shopping_list
  total_shopper_net += shopper[i].total_net_value
  total_shopper_gross += shopper[i].how_much_spent
  total_shopper_product_count += shopper[i].total_product_count

end

谁能解释一下?

4

1 回答 1

2

ieach迭代器中手动递增。任何后续引用shopper[i]尚不存在,因为购物者仅在循环顶部创建。

于 2013-10-30T23:26:01.707 回答