To find the first, second, and third numbers generated in the numbers
array, you can simply access them directly using array indexing [0]
, [1]
, and [2]
respectively. Here’s how you can modify your code to do that:
rubynumbers = 3.times.map { Random.new.rand(0..10000) }
prizes = numbers.map do |x|
case x
when 1..3000
[ '7', 10000 ]
when 3001..6000
[ "Cherries", 500 ]
when 6001..10000
[ "Diamond", 400 ]
end
end
first_number = numbers[0]
second_number = numbers[1]
third_number = numbers[2]
puts "Your results are: #{prizes.collect { |p| p[0] }.join(", ")}!"
puts "First number: #{first_number}"
puts "Second number: #{second_number}"
puts "Third number: #{third_number}"
This code will output the first, second, and third numbers generated along with the results.
المزيد من المعلومات
إذا كنت ترغب في تحديد أو استخدام الأرقام التي تم إنشاؤها لاحقًا في البرنامج، يمكنك تخزينها في متغيرات لاستخدامها لاحقًا. في هذه الحالة، يمكنك تعديل الكود كما يلي:
rubynumbers = 3.times.map { Random.new.rand(0..10000) }
prizes = numbers.map do |x|
case x
when 1..3000
[ '7', 10000 ]
when 3001..6000
[ "Cherries", 500 ]
when 6001..10000
[ "Diamond", 400 ]
end
end
# استخراج الأرقام وتخزينها في متغيرات
first_number = numbers[0]
second_number = numbers[1]
third_number = numbers[2]
# إظهار النتائج والأرقام
puts "Your results are: #{prizes.collect { |p| p[0] }.join(", ")}!"
puts "First number: #{first_number}"
puts "Second number: #{second_number}"
puts "Third number: #{third_number}"
# اختبار الأرقام إذا كانت تطابق
if first_number == second_number && second_number == third_number
puts "All numbers are the same!"
else
puts "Not all numbers are the same."
end
هذا التعديل سيقوم بتخزين الأرقام الثلاثة التي تم إنشاؤها في المتغيرات first_number
، second_number
، و third_number
، وبعد ذلك سيتم استخدامها للتحقق مما إذا كانت جميعها متطابقة أم لا.
الوسوم
Ruby أرقام عشوائية توليد فحص