简体   繁体   中英

When I run this method and then print my array, nothing gets printed

public static void assignCellTypes(char[][] tissue, int percentBlank, int percentX){


    //Your code goes here
    Random rnd = new Random();
    int row = 0;
    int col = 0;
    int rndRow = rnd.nextInt(tissue.length);
    int rndCol = rnd.nextInt(tissue.length);

    int numOfCells = (tissue.length * tissue.length);
    double numOfBlankCells = numOfCells * (percentBlank/100.0);
    numOfBlankCells = Math.ceil(numOfBlankCells);
    double numOfXCells = (numOfCells-numOfBlankCells) * (percentX/100.0);
    numOfXCells = Math.ceil(numOfXCells);
    double numOfOCells = numOfCells - (numOfBlankCells + numOfXCells);

    if (numOfCells < numOfBlankCells + numOfXCells + numOfOCells) {
        System.out.println("Percentages can't be over 100%");
        System.exit(0);
    }

    int counterBlank = 0;
    while (counterBlank <= numOfBlankCells) {
        rndRow = rnd.nextInt(tissue.length);
        rndCol = rnd.nextInt(tissue.length);
        if (tissue[rndRow][rndCol] == '\0') {
            tissue[rndRow][rndCol] = ' ';
            counterBlank++;
        }   
    }
    int counterX = 0;
    while (counterX <= numOfXCells) {
        rndRow = rnd.nextInt(tissue.length);
        rndCol = rnd.nextInt(tissue.length);
        if (tissue[rndRow][rndCol] == '\0') {
            tissue[rndRow][rndCol] = 'X';
            counterX++;
        } 
    }
    int counterO = 0;
    while (counterO <= numOfOCells) {
        rndRow = rnd.nextInt(tissue.length);
        rndCol = rnd.nextInt(tissue.length);
        if (tissue[rndRow][rndCol] == '\0') {
            tissue[rndRow][rndCol] = 'O';
            counterO++;
        }
    }       
}

I'm assuming that the problem lies in one of the loops, but I don't really know what's wrong. What I want the loops to do is to check if a random element on the array is empty. If it is I want to assign a character to that array element and then add one to the counter. Once the counter reaches the number I want it to it'll move on to the next while loop. Can anybody help me out?

Your code runs into an endless loop. As soon as you finish the loop with the blank cells, there are no more cells in tissue that are \\0 , because your loop only finishes if you hit all of them. Then you go to the next loop, and since there are no blank cells left, it will never hit on a cell that's blank, never increment counterX, and never exit the loop.

Also, your condition for checking the percentage is wrong. Since you calculated numOfOCells based on numOfCells minus the other two, numOfBlankCells + numOfXCells + numOfOCells will always be exactly numOfCells .

You need to re-think your whole strategy.

Because each while loop wants to fill 1 more cell. The last loop can't fill the required number of cells, because there are no more empty cells available. Modify the <= condition in all loops to < and it will be OK.

public static void assignCellTypes(char[][] tissue, int percentBlank, int percentX){

    //Your code goes here
    Random rnd = new Random();
    int row = 0;
    int col = 0;
    int rndRow = rnd.nextInt(tissue.length);
    int rndCol = rnd.nextInt(tissue.length);

    int numOfCells = (tissue.length * tissue.length);
    double numOfBlankCells = numOfCells * (percentBlank/100.0);
    numOfBlankCells = Math.ceil(numOfBlankCells);
    double numOfXCells = (numOfCells-numOfBlankCells) * (percentX/100.0);
    numOfXCells = Math.ceil(numOfXCells);
    double numOfOCells = numOfCells - (numOfBlankCells + numOfXCells);

    if (numOfCells < numOfBlankCells + numOfXCells + numOfOCells) {
        System.out.println("Percentages can't be over 100%");
        System.exit(0);
    }

    int counterBlank = 0;
    while (counterBlank < numOfBlankCells) {
        rndRow = rnd.nextInt(tissue.length);
        rndCol = rnd.nextInt(tissue.length);
        if (tissue[rndRow][rndCol] == '\0') {
            tissue[rndRow][rndCol] = ' ';
            counterBlank++;
        }   
    }
    int counterX = 0;
    while (counterX < numOfXCells) {
        rndRow = rnd.nextInt(tissue.length);
        rndCol = rnd.nextInt(tissue.length);
        if (tissue[rndRow][rndCol] == '\0') {
            tissue[rndRow][rndCol] = 'X';
            counterX++;
        } 
    }
    int counterO = 0;
    while (counterO < numOfOCells) {
        rndRow = rnd.nextInt(tissue.length);
        rndCol = rnd.nextInt(tissue.length);
        if (tissue[rndRow][rndCol] == '\0') {
            tissue[rndRow][rndCol] = 'O';
            counterO++;
        }
    }       
}

But what about producing the same result from the other side - pass through the cells linearly and fill the cells with the random values at once? :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM