Java Hashmap if 语句检查不起作用
Java Hashmap if statement check doesnt work
首先感谢所有愿意阅读所有这些代码并帮助我的人。
我有一个 Hashmap,它只是将一个板打印到控制台,它如下所示:
public class Square {
private char status;
public Square(String statusp) {
if(statusp.equals("empty")) {
this.status = '.';
}else if(statusp.equals("black")) {
this.status = 'X';
}else if(statusp.equals("white")) {
this.status = 'O';
}else {
System.out.println("ERROR: status can only be: empty, black, white");
}
}
public char getStatus() {
return status;
}
}
public class Board {
public HashMap<String, Square> board;
public Board() {
board = new HashMap<String, Square>();
Square empty = new Square("empty");
Square black = new Square("black");
Square white = new Square("white");
board.put("a1", empty);
board.put("a2", empty);
board.put("a3", empty);
board.put("a4", empty);
board.put("a5", empty);
board.put("a6", empty);
board.put("a7", empty);
board.put("a8", empty);
board.put("b1", empty);
board.put("b2", empty);
board.put("b3", empty);
board.put("b4", empty);
board.put("b5", empty);
board.put("b6", empty);
board.put("b7", empty);
board.put("b8", empty);
board.put("c1", empty);
board.put("c2", empty);
board.put("c3", empty);
board.put("c4", empty);
board.put("c5", empty);
board.put("c6", empty);
board.put("c7", empty);
board.put("c8", empty);
board.put("d1", empty);
board.put("d2", empty);
board.put("d3", empty);
board.put("d4", white);
board.put("d5", black);
board.put("d6", empty);
board.put("d7", empty);
board.put("d8", empty);
board.put("e1", empty);
board.put("e2", empty);
board.put("e3", empty);
board.put("e4", black);
board.put("e5", white);
board.put("e6", empty);
board.put("e7", empty);
board.put("e8", empty);
board.put("f1", empty);
board.put("f2", empty);
board.put("f3", empty);
board.put("f4", empty);
board.put("f5", empty);
board.put("f6", empty);
board.put("f7", empty);
board.put("f8", empty);
board.put("g1", empty);
board.put("g2", empty);
board.put("g3", empty);
board.put("g4", empty);
board.put("g5", empty);
board.put("g6", empty);
board.put("g7", empty);
board.put("g8", empty);
board.put("h1", empty);
board.put("h2", empty);
board.put("h3", empty);
board.put("h4", empty);
board.put("h5", empty);
board.put("h6", empty);
board.put("h7", empty);
board.put("h8", empty);
}
}
它基本上打印了这个:
这个想法是,当用户提供等于 HashMap 的键之一的输入时,该键的值将更改为方形黑色或方形白色(X 或 O),并且值仅更改如果用户输入的键还没有 Square black 或 Square white 的值。第一次检查工作正常,但我似乎无法进行第二次检查。它总是覆盖该值,即使它已经是 X 或 O。
为了执行上述检查,我有以下代码:
System.out.println(p1.getName().toUpperCase() + ", please enter your move:");
String move = io.readInput();
//check if the hashmap contains the key of move
if (board.board.containsKey(move)) {
//check if the value of key move isnt equal to Square black or white (X or O)
if(board.board.get(move) != black || board.board.get(move) != white ) {
board.board.replace(move, black);
}else {
System.out.println("that spot is already taken");
}
board.printBoard();
}
我尝试更改 ||到 && 但没有结果。
我也试过:
if(board.board.get(move) == empty) {
board.board.replace(move, black);
board.printBoard();
}else {
System.out.println("that spot is already taken");
}
但是无论我给它什么输入总是returns else 语句和 if 语句永远不会为真,即使 move 等于空。
知道为什么第二个 if 语句不起作用吗?它给了我零错误。
首先,我建议你做以下测试(不解决问题,但最终会显示真正的问题 - 与 HashMap
无关):
System.out.println(new Square("black") != new Square("black"));
要比较对象(按内容),您应该使用 equals
,就像实际上在 Square
中所做的那样。原因:==
和!=
,应用于对象时,不比较内容,只比较是否是同一个实例(同一个内存)。 new
如果没有突然终止,将始终创建一个新实例。 new String("abc") != new String("abc")
的原因相同
解决方案:add/implement/override Square
中的 equals
方法并用它来比较它们。此方法将比较存储在 status
.
中的值
注意:在覆盖 equals
方法时,还建议覆盖 hashCode
方法。
解决方法:声明常量 - 仅一次,全局可用,随处使用:
public final Square BLACK = new Square("black");
...
如果一直使用,这些可以与 ==
和 !=
进行比较 - 仍然建议实施 equals
。
由于这是一个危险的解决方案,一些开发人员可能会创建一个新的实例并且它会再次失败...请参阅下一个解决方案。
更好的解决方案:为这 3 个值创建一个 enum
:
public enum Square {
EMPTY, BLACK, WHITE;
}
不需要 equals
并且可以直接与 ==
和 !=
(或 equals
)进行比较(但需要覆盖 toString
或实现一些正确输出的方法)
假设代码有 white
、black
和 empty
的不同实例,基于 board.board.get(move) == empty
总是返回 false
注意:还要检查 Joop 的 !!
if (board.board.get(move) != black || board.board.get(move) != white)
此模式:!CASE_A || !CASE_B
始终为真(因为其中一种情况为假)。
使用
if (board.board.get(move) != black && board.board.get(move) != white)
首先感谢所有愿意阅读所有这些代码并帮助我的人。
我有一个 Hashmap
public class Square {
private char status;
public Square(String statusp) {
if(statusp.equals("empty")) {
this.status = '.';
}else if(statusp.equals("black")) {
this.status = 'X';
}else if(statusp.equals("white")) {
this.status = 'O';
}else {
System.out.println("ERROR: status can only be: empty, black, white");
}
}
public char getStatus() {
return status;
}
}
public class Board {
public HashMap<String, Square> board;
public Board() {
board = new HashMap<String, Square>();
Square empty = new Square("empty");
Square black = new Square("black");
Square white = new Square("white");
board.put("a1", empty);
board.put("a2", empty);
board.put("a3", empty);
board.put("a4", empty);
board.put("a5", empty);
board.put("a6", empty);
board.put("a7", empty);
board.put("a8", empty);
board.put("b1", empty);
board.put("b2", empty);
board.put("b3", empty);
board.put("b4", empty);
board.put("b5", empty);
board.put("b6", empty);
board.put("b7", empty);
board.put("b8", empty);
board.put("c1", empty);
board.put("c2", empty);
board.put("c3", empty);
board.put("c4", empty);
board.put("c5", empty);
board.put("c6", empty);
board.put("c7", empty);
board.put("c8", empty);
board.put("d1", empty);
board.put("d2", empty);
board.put("d3", empty);
board.put("d4", white);
board.put("d5", black);
board.put("d6", empty);
board.put("d7", empty);
board.put("d8", empty);
board.put("e1", empty);
board.put("e2", empty);
board.put("e3", empty);
board.put("e4", black);
board.put("e5", white);
board.put("e6", empty);
board.put("e7", empty);
board.put("e8", empty);
board.put("f1", empty);
board.put("f2", empty);
board.put("f3", empty);
board.put("f4", empty);
board.put("f5", empty);
board.put("f6", empty);
board.put("f7", empty);
board.put("f8", empty);
board.put("g1", empty);
board.put("g2", empty);
board.put("g3", empty);
board.put("g4", empty);
board.put("g5", empty);
board.put("g6", empty);
board.put("g7", empty);
board.put("g8", empty);
board.put("h1", empty);
board.put("h2", empty);
board.put("h3", empty);
board.put("h4", empty);
board.put("h5", empty);
board.put("h6", empty);
board.put("h7", empty);
board.put("h8", empty);
}
}
它基本上打印了这个:
这个想法是,当用户提供等于 HashMap 的键之一的输入时,该键的值将更改为方形黑色或方形白色(X 或 O),并且值仅更改如果用户输入的键还没有 Square black 或 Square white 的值。第一次检查工作正常,但我似乎无法进行第二次检查。它总是覆盖该值,即使它已经是 X 或 O。
为了执行上述检查,我有以下代码:
System.out.println(p1.getName().toUpperCase() + ", please enter your move:");
String move = io.readInput();
//check if the hashmap contains the key of move
if (board.board.containsKey(move)) {
//check if the value of key move isnt equal to Square black or white (X or O)
if(board.board.get(move) != black || board.board.get(move) != white ) {
board.board.replace(move, black);
}else {
System.out.println("that spot is already taken");
}
board.printBoard();
}
我尝试更改 ||到 && 但没有结果。 我也试过:
if(board.board.get(move) == empty) {
board.board.replace(move, black);
board.printBoard();
}else {
System.out.println("that spot is already taken");
}
但是无论我给它什么输入总是returns else 语句和 if 语句永远不会为真,即使 move 等于空。
知道为什么第二个 if 语句不起作用吗?它给了我零错误。
首先,我建议你做以下测试(不解决问题,但最终会显示真正的问题 - 与 HashMap
无关):
System.out.println(new Square("black") != new Square("black"));
要比较对象(按内容),您应该使用 equals
,就像实际上在 Square
中所做的那样。原因:==
和!=
,应用于对象时,不比较内容,只比较是否是同一个实例(同一个内存)。 new
如果没有突然终止,将始终创建一个新实例。 new String("abc") != new String("abc")
解决方案:add/implement/override Square
中的 equals
方法并用它来比较它们。此方法将比较存储在 status
.
注意:在覆盖 equals
方法时,还建议覆盖 hashCode
方法。
解决方法:声明常量 - 仅一次,全局可用,随处使用:
public final Square BLACK = new Square("black");
...
如果一直使用,这些可以与 ==
和 !=
进行比较 - 仍然建议实施 equals
。
由于这是一个危险的解决方案,一些开发人员可能会创建一个新的实例并且它会再次失败...请参阅下一个解决方案。
更好的解决方案:为这 3 个值创建一个 enum
:
public enum Square {
EMPTY, BLACK, WHITE;
}
不需要 equals
并且可以直接与 ==
和 !=
(或 equals
)进行比较(但需要覆盖 toString
或实现一些正确输出的方法)
假设代码有 white
、black
和 empty
的不同实例,基于 board.board.get(move) == empty
总是返回 false
注意:还要检查 Joop 的
if (board.board.get(move) != black || board.board.get(move) != white)
此模式:!CASE_A || !CASE_B
始终为真(因为其中一种情况为假)。
使用
if (board.board.get(move) != black && board.board.get(move) != white)