vendredi 7 octobre 2016

Cows and Bulls repetition

I've been attempting to write a cows and bulls program over the last few hours. In cows and bulls. The program is supposed to generate 4 non-repeating integers between 0 and 9 for the user to guess, but my program outputs a group of integers with some repeating values about 1 in 10 times. Everyone I've gone to hasn't found any errors, if you know a fix please help me. My program so far is as follows:

    Random rng = new Random();
    int pos1 = rng.nextInt(10);
    int pos2 = rng.nextInt(10);
    int pos3 = rng.nextInt(10);
    int pos4 = rng.nextInt(10);
    int norepeat = 0;

    while(norepeat == 0){
        if(pos1 == pos3 || pos1 == pos2 || pos1 == pos4){
            pos4 = rng.nextInt(10);
        }
        if(pos2 == pos1 || pos2 == pos3 || pos2 == pos4){
            pos2 = rng.nextInt(10);
        }
        if(pos3 == pos1 || pos3 == pos2 || pos3 == pos4){ 
            pos3 = rng.nextInt(10);
        }
        if(pos4 == pos3 || pos4 == pos2 || pos4 == pos1){
            pos4 = rng.nextInt(10);
        }
        else {
            norepeat = 1;
        }
}


    System.out.print(pos1);
    System.out.print(" " + pos2);
    System.out.print(" " + pos3);
    System.out.print(" " + pos4);




Aucun commentaire:

Enregistrer un commentaire