Java 将int数组中的整数与用户提供的整数进行比较
我有一个二维数组,gridArray[5][7],填充了数字(只有最后一行填充了除0以外的任何内容)。我获取用户输入的列和颜色,将颜色转换为数字表示,然后对照数组中的行进行检查。如果它在用户输入的列中,它应该打印“您的猜测是正确的”。相反,它似乎打印“你的猜测是错误的”,即使猜测是正确的。我检查了布尔值,当它应该返回true时,它返回false。我不明白为什么。这是我的密码:Java 将int数组中的整数与用户提供的整数进行比较,java,arrays,Java,Arrays,我有一个二维数组,gridArray[5][7],填充了数字(只有最后一行填充了除0以外的任何内容)。我获取用户输入的列和颜色,将颜色转换为数字表示,然后对照数组中的行进行检查。如果它在用户输入的列中,它应该打印“您的猜测是正确的”。相反,它似乎打印“你的猜测是错误的”,即使猜测是正确的。我检查了布尔值,当它应该返回true时,它返回false。我不明白为什么。这是我的密码: public static void main(String[] args) { int[][] gri
public static void main(String[] args) {
int[][] gridArray = new int[7][5];
gridPop(gridArray);
playFunc(gridArray);
}
public static void playFunc(int[][] gridArray) {
char colorGuess = 'N';
int columnGuess = -1;
String columnString = "a column to guess";
boolean correctIncorrectGuess = false;
System.out.println("Please enter a color to guess (R/B/P/Y/G)");
colorGuess = charCheck();
System.out.println("Please enter a column to guess");
System.out.println("The column # must be between 1 and 5");
columnGuess = intChecker(columnString);
correctIncorrectGuess = checkHat(gridArray, charReturn(colorGuess), columnGuess);
System.out.println("Your guess was " + boolCorrectString(correctIncorrectGuess));
}
public static boolean checkHat(int[][] gridArray, int color, int column){
boolean guess = false;
for (int i = 0; i < gridArray.length; i++) {
if (gridArray[i][column] == color) {
guess = true;
} else {
guess = false;
}
}
return guess;
}
public static int intChecker(String object) {
boolean correctInput = false;
int userInput = 0;
while (!correctInput) {
try {
userInput = scanner.nextInt();
correctInput = true;
} catch(InputMismatchException e) {
System.out.println("I'm sorry, that doesn't seem to be a number");
System.out.println("Please enter " +object);
scanner.next();
}
}
return userInput;
}
public static String boolCorrectString(boolean toString) {
String correctInc = "";
if (toString) {
correctInc = "correct";
} else {
correctInc = "incorrect";
}
return correctInc;
}
public static int charReturn(char color) {
int colorToInt = -1;
if (color == 'G') {
colorToInt = 1;
} else if (color == 'R') {
colorToInt = 2;
} else if (color == 'B') {
colorToInt = 3;
} else if (color == 'P') {
colorToInt = 4;
} else if (color == 'Y') {
colorToInt = 5;
} else if (color == 'g') {
colorToInt = 1;
} else if (color == 'r') {
colorToInt = 2;
} else if (color == 'b') {
colorToInt = 3;
} else if (color == 'p') {
colorToInt = 4;
} else if (color == 'y') {
colorToInt = 5;
}
return colorToInt;
}
public static char charCheck() {
char userInput = 'N';
userInput = scanner.next().charAt(0);
while((userInput != 'G') && (userInput != 'Y') && (userInput != 'B')
&& (userInput != 'R') && (userInput != 'P') && (userInput != 'g') &&
(userInput != 'y') && (userInput != 'b') && (userInput != 'r') && (userInput != 'p')) {
System.out.println("Please enter R/B/P/Y/G");
userInput = scanner.next().charAt(0);
}
return userInput;
}
publicstaticvoidmain(字符串[]args){
int[][]gridArray=新int[7][5];
gridPop(gridArray);
playFunc(网格阵列);
}
公共静态void playFunc(int[][]gridArray){
char colorGuess='N';
int columnGuess=-1;
String columnString=“要猜测的列”;
布尔correctIncorrectGuess=假;
System.out.println(“请输入要猜测的颜色(R/B/P/Y/G)”;
colorGuess=charCheck();
System.out.println(“请输入一列进行猜测”);
System.out.println(“列#必须介于1和5之间”);
columnGuess=intChecker(columnString);
correctIncorrectGuess=checkHat(gridArray、charReturn(colorGuess)、columnGuess);
println(“您的猜测是”+boolCorrectString(correctIncorrectGuess));
}
公共静态布尔checkHat(int[][]网格数组、int颜色、int列){
布尔猜测=假;
for(int i=0;i
我做了一些测试,它为列收集了正确的数字,颜色的表示,等等。看起来,即使gridArray[I][column]应该与猜测的颜色相同,布尔值也会返回为false(我测试过,它没有打印错误——当它应该为true时,它实际上会找到false)。为什么代码不能正确比较gridArray条目和颜色猜测?代码在哪里分解
编辑:
网格法
public static void gridPop(int[][] gridArray) {
for (int i=0; i < gridArray[6].length; i++) {
int randomNum = ThreadLocalRandom.current().nextInt(1, 6);
gridArray[6][i] = randomNum;
}
for (int i=5; i >= 0; i--) {
for (int j=0; j < gridArray[i].length; j++) {
gridArray[i][j] = 0;
}
}
}
publicstaticvoidgridpop(int[][]gridArray){
对于(int i=0;i=0;i--){
对于(int j=0;j
编辑:其实我很笨。这是一个off by one错误,因为它是在检查用户输入的列,而不是用户输入的列-1您的问题在于checkHat()方法。首次找到匹配元素时,将布尔猜测设置为true;但是,当选中的下一个值不等于“颜色猜测”时,该值将设置回false。像这样的方法应该会奏效:
public static boolean checkHat(int[][] gridArray, int color, int column){
for (int i = 0; i < gridArray.length; i++) {
if (gridArray[i][column] == color) {
return true;
}
}
return false;
}
公共静态布尔checkHat(int[][]网格数组、int颜色、int列){
for(int i=0;i
在guess=true之后放置一个break
编码>并使用适当的值初始化int[][]gridArray
;默认情况下,此数组的所有索引都为零。在这里,您将零与非零的颜色的值进行比较。所以它总是假的。我尝试在将布尔猜测更改为true后添加一个中断,但仍然不起作用。这会有什么不同吗?我有一个函数,应该是随机数组中的数字。我将把它添加到文本中。