为什么我的随机数生成器在使用它输入二维数组的值时变成未使用的分配?
Why is my random number generator becoming an unused assignment when using it to input values for 2d array?
我一直在制作一个基本的战舰游戏,为了让 CPU 在二维数组上选择船只位置,我正在制作两个随机数生成器,以使用单独的方法从 1 到 8 中选取值。出于某种原因,随机生成器显示为未使用,我不知道为什么。到目前为止,这就是我所拥有的。如果有人知道我做错了什么,请告诉我。
//calls method
board3 = CPUship(board3);
//displays result of method
for (int i = 0; i < board3.length; i++) {
for (int j = 0; j < board3.length; j++) {
System.out.print(board3[i][j]);
}
System.out.println("");
}
public static String[][] CPUship(String[][] board3) {
int rowGenerate;
int colGenerate;
boolean valid2 = false;
for (int CPUships = 0; CPUships < 6; CPUships++) {
while (!valid2) {
rowGenerate = (int) (9 * Math.random() + 0);
colGenerate = (int) (9 * Math.random() + 0);
if (!board3[rowGenerate][colGenerate].equalsIgnoreCase("#")) {
board3[rowGenerate][colGenerate] = "#";
valid2 = true;
}
}
valid2 = false;
}
return board3;
}
这里应该是完整的代码
public static String[][] CPUship(String[][]board3){
int rowGenerate;
int colGenerate;
for (int CPUships = 0; CPUships < 6; CPUships++) {
boolean valid2=false;
while (!valid2){ //instead of valid = false "
rowGenerate = (int) ( 9* Math.random() + 0);
colGenerate = (int) ( 9* Math.random() + 0);
if (!board3[rowGenerate][colGenerate].equalsIgnoreCase ("#")){
board3[rowGenerate][colGenerate]="#";
valid2=true;
}
//removed the valid2=false, it does not "reset" automatically
//removed the CPUships++ ... already done by the for loop
}
}
return board3;
}
或使用 break 代替有效的布尔值
public static String[][] CPUship(String[][]board3){
int rowGenerate;
int colGenerate;
for (int CPUships = 0; CPUships < 6; CPUships++) {
while (true){ //instead of valid = false "
rowGenerate = (int) ( 9* Math.random() + 0);
colGenerate = (int) ( 9* Math.random() + 0);
if (!board3[rowGenerate][colGenerate].equalsIgnoreCase ("#")){
board3[rowGenerate][colGenerate]="#";
break;
}
//removed the valid2=false, it does not "reset" automatically
//removed the CPUships++ ... already done by the for loop
}
}
return board3;
}
我一直在制作一个基本的战舰游戏,为了让 CPU 在二维数组上选择船只位置,我正在制作两个随机数生成器,以使用单独的方法从 1 到 8 中选取值。出于某种原因,随机生成器显示为未使用,我不知道为什么。到目前为止,这就是我所拥有的。如果有人知道我做错了什么,请告诉我。
//calls method
board3 = CPUship(board3);
//displays result of method
for (int i = 0; i < board3.length; i++) {
for (int j = 0; j < board3.length; j++) {
System.out.print(board3[i][j]);
}
System.out.println("");
}
public static String[][] CPUship(String[][] board3) {
int rowGenerate;
int colGenerate;
boolean valid2 = false;
for (int CPUships = 0; CPUships < 6; CPUships++) {
while (!valid2) {
rowGenerate = (int) (9 * Math.random() + 0);
colGenerate = (int) (9 * Math.random() + 0);
if (!board3[rowGenerate][colGenerate].equalsIgnoreCase("#")) {
board3[rowGenerate][colGenerate] = "#";
valid2 = true;
}
}
valid2 = false;
}
return board3;
}
这里应该是完整的代码
public static String[][] CPUship(String[][]board3){
int rowGenerate;
int colGenerate;
for (int CPUships = 0; CPUships < 6; CPUships++) {
boolean valid2=false;
while (!valid2){ //instead of valid = false "
rowGenerate = (int) ( 9* Math.random() + 0);
colGenerate = (int) ( 9* Math.random() + 0);
if (!board3[rowGenerate][colGenerate].equalsIgnoreCase ("#")){
board3[rowGenerate][colGenerate]="#";
valid2=true;
}
//removed the valid2=false, it does not "reset" automatically
//removed the CPUships++ ... already done by the for loop
}
}
return board3;
}
或使用 break 代替有效的布尔值
public static String[][] CPUship(String[][]board3){
int rowGenerate;
int colGenerate;
for (int CPUships = 0; CPUships < 6; CPUships++) {
while (true){ //instead of valid = false "
rowGenerate = (int) ( 9* Math.random() + 0);
colGenerate = (int) ( 9* Math.random() + 0);
if (!board3[rowGenerate][colGenerate].equalsIgnoreCase ("#")){
board3[rowGenerate][colGenerate]="#";
break;
}
//removed the valid2=false, it does not "reset" automatically
//removed the CPUships++ ... already done by the for loop
}
}
return board3;
}