I think I must be losing it. I'm trying to locate the coordinates of an element in a 2D Array.
我想我一定是要失去它。我正在尝试在2D数组中找到元素的坐标。
I've dumbed the code down to as simple as I could and still can't get it right. I'm very new to Java
我把代码简化为尽可能简单,但仍然无法正确完成。我是Java的新手
Please tell me why the answer to this is always 42.0, no matter where I put the ' * '
请告诉我为什么答案总是42.0,无论我把'*'放在哪里
public static void main(String[] args) {
locateStar(board);
}
static char[][] board = {
{ '.', '.', '.', '.' },
{ '.', '.', '.', '.' },
{ '.', '.', '.', '*' },
{ '.', '.', '.', '.' }
};
public static void locateStar(char[][] board) {
double star = 0;
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
if (board[i][j] == '*') {
star = board[i][j];
}
}
}
System.out.println(star);
}
3 个解决方案
#1
4
Look closely at this line:
仔细看看这一行:
star = board[i][j];
You are assigning a char
to a double
. The value 42
is the ASCII value of an asterisk *
. If you want to print out the coordinates (which are two values, not just one), then try this:
您正在为double分配char。值42是星号*的ASCII值。如果要打印出坐标(两个值,而不是一个),请尝试以下方法:
public static void locateStar(char[][] board) {
int x, y;
for (int i=0; i < board.length; i++) {
for (int j=0; j < board[0].length; j++) {
if (board[i][j] == '*') {
x = i;
y = j;
}
}
}
System.out.println("Found a star at (" + x + ", " + y + ")");
}
#2
2
Ahah it's kinda funny :) You find the star, and you assign the star to a double value:
啊,它有点好笑:)你找到了星星,你将星星指定为双倍值:
star = board[i][j];
That is, you assign '*'
to a double, getting the ASCII value of the * character, which is - in fact - 42.
也就是说,你将'*'分配给double,得到*字符的ASCII值,实际上是-42。
Here's some code that shows that it's found:
这里有一些代码显示它被发现:
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
if (board[i][j] == '*') {
System.out.println("Found at " + i + " " + j);
break;
}
}
}
#3
2
The ASCII value 42 corresponds to the * symbol. Your code is in effect retrieving the value * and then casting it implicitly to a number, which will always be 42. You are not going to see a difference regardless of position, because you are only looking for the '*' value and not its position or anything else.
ASCII值42对应于*符号。你的代码实际上是检索值*然后隐式地将它强制转换为一个数字,它总是为42.你不会看到任何位置的差异,因为你只是寻找'*'值,而不是它的位置或其他任何东西。
#1
4
Look closely at this line:
仔细看看这一行:
star = board[i][j];
You are assigning a char
to a double
. The value 42
is the ASCII value of an asterisk *
. If you want to print out the coordinates (which are two values, not just one), then try this:
您正在为double分配char。值42是星号*的ASCII值。如果要打印出坐标(两个值,而不是一个),请尝试以下方法:
public static void locateStar(char[][] board) {
int x, y;
for (int i=0; i < board.length; i++) {
for (int j=0; j < board[0].length; j++) {
if (board[i][j] == '*') {
x = i;
y = j;
}
}
}
System.out.println("Found a star at (" + x + ", " + y + ")");
}
#2
2
Ahah it's kinda funny :) You find the star, and you assign the star to a double value:
啊,它有点好笑:)你找到了星星,你将星星指定为双倍值:
star = board[i][j];
That is, you assign '*'
to a double, getting the ASCII value of the * character, which is - in fact - 42.
也就是说,你将'*'分配给double,得到*字符的ASCII值,实际上是-42。
Here's some code that shows that it's found:
这里有一些代码显示它被发现:
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
if (board[i][j] == '*') {
System.out.println("Found at " + i + " " + j);
break;
}
}
}
#3
2
The ASCII value 42 corresponds to the * symbol. Your code is in effect retrieving the value * and then casting it implicitly to a number, which will always be 42. You are not going to see a difference regardless of position, because you are only looking for the '*' value and not its position or anything else.
ASCII值42对应于*符号。你的代码实际上是检索值*然后隐式地将它强制转换为一个数字,它总是为42.你不会看到任何位置的差异,因为你只是寻找'*'值,而不是它的位置或其他任何东西。