我正在 NetBeans 中创建一个程序,其中用户与计算机对手玩剪刀石头布。
计算机不会随机选择石头布或剪刀,而是会根据上一轮的事件有不同的偏好 - 即输给石头使它更有可能玩布。
我想通过创建一个带有 2D 数组的 3D 数组来实现这一点,以表示计算机获胜和计算机失败的情况 - 它们本身都有一个石头、剪刀、布的数组。
稍后尝试使用数组时遇到了问题;特别是使用 .length() 获取随机变量,然后从该位置提取数字。
当将鼠标悬停在 Netbeans 上时,Netbeans 只是显示错误“找不到符号” - 尽管我将数组声明为公共(public)数组和静态数组。
我尝试创建一个与内部二维数组内的数组之一相等的临时数组,但遇到了相同的错误。
public static int[][][] moveArrays = {
//Turn 1
{
{1,1,1,2,2,2,3,3,3},
{1,1,2,2,2,2,3,3,3,3,3,3},
{1,2,2,2,2,2,3,3},
},
//Opponent Wins
{
//With Rock
{1,1,1,1,1,2,3,3,3,3,3,},
//With Paper
{1,1,2,2,2,2,2,2,3},
//With Scissors
{1,1,2,2,3,3,3,3},
},
//Opponent Loses
{
//To Rock
{1,1,1,1,1,1,2,2,2,2,3},
//To Paper
{1,2,2,2,2,2,2,3,3,3},
//To Scissors
{1,1,1,1,1,1,2,3,3,3,3,},
}
};
public void turn1() {
int rnd = new Random().nextInt(3);
switch (rnd) {
case 0:
//int[] clone = moveArray[0][0];
int rnd2 = new Random().nextInt(moveArray[0][0].length());
enemyChoice = moveArray[0][0][rnd2];
break;
case 1:
int rnd2 = new Random().nextInt(moveArray[0][1].length());
enemyChoice = moveArray[0][1][rnd2];
break;
case 2:
int rnd2 = new Random().nextInt(moveArray[0][2].length());
enemyChoice = moveArray[0][2][rnd2];
break;
}
}
每次使用moveArray都会出现“Can't find Symbol”的错误。
最佳答案
The error "Can't find Symbol" occurs every time moveArray is used.
正确。您已将数组声明为 moveArrays
,而不是 moveArray
。此外,数组有一个 .length
变量,而不是 .length()
方法。 “找不到符号”意味着您输入错误,和/或尚未定义您尝试使用的引用。
关于java - 如何修复: Net Beans Can't Find 3D Array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58169945/