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++;
}
}
}
我假設問題出在一個循環,但我真的不知道什麼是錯的。我想循環做的是檢查數組上的隨機元素是否爲空。如果它是我想分配一個字符到該數組元素,然後將一個添加到計數器。一旦計數器達到我想要的數字,它就會轉到下一個while循環。有人可以幫我嗎?當我運行此方法,然後打印我的陣列,沒有打印
您應該嘗試一些打印調試,以確保您正確輸入while while循環,並且值是您所期望的。 – 2014-11-03 19:11:39
下一次真的做調試(http://ericlippert.com/2014/03/05/how-to-debug-small-programs/),並提出更好的情況。它不只是「不打印任何東西」,它在第三個「while」時陷入了無限循環。 – Deltharis 2014-11-03 19:34:13
對不起,我一定會在下次調試。感謝您的幫助! – RogueWolf 2014-11-03 20:12:25