mercredi 31 juillet 2019

How can I re-prompt a random number generator if it does not give me the number I want

I basically set up a boolean matrix that goes up to 5x5 dimensions and defined each box in the matrix to be either true or false.

to display:

    {
    s[0][0] = false; s[0][1] = true; s[0][2] = false; s[0][3] = true; s[0][4] = false; s[0][5] = false;
    s[1][0] = false; s[1][1] = true; s[1][2] = false; s[1][3] = false; s[1][4] = false; s[1][5] = true;
    s[2][0] = true; s[2][1] = false; s[2][2] = true; s[2][3] = true; s[2][4] = true; s[2][5] = true;
    s[3][0] = true; s[3][1] = false; s[3][2] = true; s[3][3] = false; s[3][4] = false; s[3][5] = true;
    s[4][0] = false; s[4][1] = true; s[4][2] = false; s[4][3] = true; s[4][4] = true; s[4][5] = true;
    s[5][0] = false; s[5][1] = true; s[5][2] = true; s[5][3] = false; s[5][4] = true; s[5][5] = false;
    }

Have tried making the while loop into an if statement, but that caused other problems.

    public void matrix(){

        Random r = new Random();

        for(j=0;j<5;j++){
            i = r.nextInt(6);
            while(s[i][j]){
                if(i==0){System.out.println((j+1) + " period: Teach kindergarden.");}
                if(i==1){System.out.println((j+1) + " period: Teach " + i + "st grade.");}
                if(i==2){System.out.println((j+1) + " period: Teach " + i + "nd grade.");}
                if(i==3){System.out.println((j+1) + " period: Teach " + i + "rd grade.");}
                if(i==4){System.out.println((j+1) + " period: Teach " + i + "th grade.");}
                if(i==5){System.out.println((j+1) + " period: Teach " + i + "th grade.");}
                else{System.out.println();}
                break;
            }
        }
    }

How can I make it where if s[i][j] is false, then the code goes back to the generator and keeps asking for an i value where s[i][j] would be TRUE?




Aucun commentaire:

Enregistrer un commentaire