You have three identical prizes to give away and a pool of 10 finalist. The fina
ID: 3673294 • Letter: Y
Question
You have three identical prizes to give away and a pool of 10 finalist. The finalists are assigned numbers from 1 to 10. Write a program to randomly select the numbers of three finalists to receive a price. Make sure not to pick the same number twice. For example, picking finalist 3, 6, 2 would be valid but picking 3,3,11 would be invalid because finalist 3 is listed twice and 11 is not a valid finalist number. The following line of code can be used to generate a random number between 1 and 10:
int num = (int) (Math.random() * 10) +1;
Explanation / Answer
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
int num1;
num1 = (int) (Math.random() * 10) +1;
int num2 = num1;
while(num1==num2)
num2 = (int) (Math.random() * 10) +1;
int num3 = num2;
while(num3==num1||num3==num2)
num3 = (int) (Math.random() * 10) +1;
System.out.println("First prize goes to finalist "+ num1);
System.out.println("Second prize goes to finalist "+ num2);
System.out.println("Third prize goes to finalist "+ num3);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.