[英]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++;
}
}
}
我以為問題出在其中一個循環中,但是我真的不知道出什么問題了。 我想要循環執行的操作是檢查數組上的隨機元素是否為空。 如果是的話,我想給該數組元素分配一個字符,然后將其添加到計數器。 一旦計數器達到所需的數字,它將繼續進行下一個while循環。 有人可以幫我嗎?
您的代碼陷入無限循環。 用空白單元格結束循環后, tissue
中不再有\\0
單元格,因為只有敲擊所有單元格,循環才會結束。 然后進入下一個循環,由於沒有空白單元格,它將永遠不會命中空白單元格,永遠不會增加counterX,也永遠不會退出循環。
另外,您檢查百分比的條件是錯誤的。 由於您numOfOCells
根據numOfCells
減去其他兩個來計算numOfOCells
的,因此numOfBlankCells + numOfXCells + numOfOCells
將始終完全是numOfCells
。
您需要重新考慮整個策略。
因為每個while循環都希望再填充1個單元格。 最后一個循環無法填充所需數量的單元格,因為沒有更多的空單元格可用。 將所有循環中的<=條件修改為<即可。
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++;
}
}
}
但是如何從另一端產生相同的結果-線性穿過像元並立即用隨機值填充像元呢? :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.