mardi 10 juillet 2018

Random matching without repeat

I've worked on the project in my school and stuck with an error. I cannot run this code since it has an error 'randA2 is already defined in method main(String[])' How can I fix it?

String [] A = {"Russia", "Saudi_Arabia", "Egypt", "Uruguay"};
int A1 = A.length;
int randA1 = (int)(Math.random()*A1);
int randA2 = (int)(Math.random()*A1);
int randA3 = (int)(Math.random()*A1);
int randA4 = (int)(Math.random()*A1);
while(randA1 == randA2) {
    int randA2 = (int)(Math.random()*A1);
}
while(randA1 == randA3) {
    int randA3 = (int)(Math.random()*A1);
}
while(randA2 == randA3) {
    int randA3 = (int)(Math.random()*A1);
}
while(randA1 == randA4) {
    int randA4 = (int)(Math.random()*A1);
}
while(randA2 == randA4) {
    int randA4 = (int)(Math.random()*A1);
}
while(randA3 == randA4) {
    int randA4 = (int)(Math.random()*A1);
}
String AnnounceA1 = A[randA1] +" " + "VS" + " " + A[randA2];
System.out.println(AnnounceA1);
String AnnounceA2 = A[randA3] +" " + "VS" + " " + A[randA4];
System.out.println(AnnounceA2);




Aucun commentaire:

Enregistrer un commentaire