gpt4 book ai didi

java - 数组代码故障排除 - 我的代码有什么问题?

转载 作者:行者123 更新时间:2023-12-01 12:13:57 24 4
gpt4 key购买 nike

编辑编辑编辑好吧,我已经简化了 for 循环,但现在我似乎在单元格分配方面遇到了问题。

 int itemsX = (int) n*n*(percentX/100);
int itemsBlank = (int) n*n*(percentBlank/100);
int itemsO = (n*n) - (itemsBlank + itemsX);
// Now we construct the grid.
for (i = 0; i < n; i++) {
for ( j = 0; j< n; j++) {
double q = Math.random();
// In this first set of If/Else's, the program will
// use a random number generator to place our items.
if (q >= .66 && a < itemsBlank) {
tissue[i][j] = ' ';
a = a + 1;
// In the event the Program has used up all of one particular item in a grid before finishing,
//it will redirect the randomized value to a different item to be put in place.
}if (q >= .66 && a == itemsBlank){
q = q - .33; }
if (q < .66 && q >= .33 && b < itemsO) {

tissue[i][j] = 'O';
b = b + 1;
if (q <.66 && q >= .33 && b == itemsO)
{ q = q - .33;
}if (q < .33 && c < itemsX) {
tissue[i][j] = 'X';
c = c + 1;
}if (q < .33 && c == itemsX)
{q = q + .66;
}
if(q >= .66 && a < itemsBlank) {
tissue[i][j] = ' ';
a = a + 1;
}
System.out.print(tissue[i][j]);
}
}System.out.println();
}
}

另外,我删除了多余的文字,因为我觉得文字墙很难看。

编辑编辑从头开始,我想我明白可能是什么问题了。我将无法使用简单的方法来打印数组,因为我被禁止使用 java.util.scanner 库,所以我必须手动打印它。所以我认为我的问题是我需要简单地为此设计一个双 for 循环。我会看看我是否可以自己做,但任何额外的意见将不胜感激。

编辑:特别感谢 rgettman 帮助我了解如何打印数组。

最佳答案

数组也是对象,但它们不会覆盖 Object's toString() method ,它负责输出 [[C@36d98810.

In other words, this method returns a string equal to the value of:

getClass().getName() + '@' + Integer.toHexString(hashCode())

因为它是一个多维数组,所以你应该调用 Arrays.deepToString输出整个数组的内容。

System.out.println(Arrays.deepToString(tissue));

关于java - 数组代码故障排除 - 我的代码有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27116230/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com