I'm writing a program in the spirit of power ball. My program asks the user how many groups do they want to generate. It can be only a number between 1-5 and if they go above 5 the programs tells them to enter a number again. A group consists of two subgroups. The first subgroup has five random unique numbers between 1-69. The second subgroup has only one random number from 1-26. After they enter the number of groups they want the program should ask the user if they want to generate another set of groups. If they say yes the programs reruns until they say no.
I'm almost done with my program. The only problem I'm having is sometimes the numbers in subgroup 1 are repeating which they shouldn't. Also I tried using while loop to repeat the program until they say no but it doesn't work. It repeats it but doesn't ask the user if they want to generate another group after it's done repeating.
{`public static void main(String[] args) {
Random rand=new Random();
Scanner input=new Scanner(System.in);
System.out.println("How many groups do you want? Enter a number between 1-5");
int n=input.nextInt();
while(n<1||n>5){
System.out.println("Enter number again");
n=input.nextInt();
}
for(int i=1;i<=n;i++){
clas clasobject=new clas();
clasobject.powerball();
}
System.out.println("Do you want to generate another group of numbers");
String answer=input.next();
if(answer.equals("yes")){
System.out.println("How many groups do you want? Enter a number between 1-5");
int y=input.nextInt();
while(y<1||y>5){
System.out.println("Enter number again");
y=input.nextInt();
}
for(int i=1;i<=y;i++){
clas clasobject=new clas();
clasobject.powerball();
}
}`
{public class clas {
Random rand=new Random();
public void powerball(){
int subgroup1[]=new int[5];
int sub2[]=new int[1];
for(int i=0;i<1;i++){
sub2[i]=rand.nextInt(26)+1;
}
for(int i=0;i<5;i++){
subgroup1[i]=rand.nextInt(69)+1;
}
int i=0;
while(i<5){
if(subgroup1[i]==subgroup1[i]){
subgroup1[i]=rand.nextInt(69)+1;
}
i++;
}
Arrays.sort(subgroup1);
System.out.println(Arrays.toString(subgroup1)+Arrays.toString(sub2));
}} }
Aucun commentaire:
Enregistrer un commentaire