Java Hashmap值检查 [英] Java Hashmap value check

查看:54
本文介绍了Java Hashmap值检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,感谢所有愿意阅读所有代码并为我提供帮助的人.

First of all thanks everyone who is willing to read all this code and help me.

我有一个Hashmap< String,Square>只需在控制台上打印一块木板,就可以完成以下操作:

I have a Hashmap<String,Square> that simply prints a board to the console and it goes as follows:

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);
    
    
    
}

public void printBoard() {
    System.out.println("+----------------+");
    System.out.println("|    abcdefgh    |");
    
    
    System.out.println("|  1 " + board.get("a1").getStatus() + "" + board.get("b1").getStatus() + board.get("c1").getStatus() + board.get("d1").getStatus()
                     + board.get("e1").getStatus() + board.get("f1").getStatus() + board.get("g1").getStatus() + board.get("h1").getStatus() + " 1  |");
    
    System.out.println("|  2 " + board.get("a2").getStatus() + "" + board.get("b2").getStatus() + board.get("c2").getStatus() + board.get("d2").getStatus()
             + board.get("e2").getStatus() + board.get("f2").getStatus() + board.get("g2").getStatus() + board.get("h2").getStatus() + " 2  |");
    
    System.out.println( "|  3 " + board.get("a3").getStatus() + "" + board.get("b3").getStatus() + board.get("c3").getStatus() + board.get("d3").getStatus()
             + board.get("e3").getStatus() + board.get("f3").getStatus() + board.get("g3").getStatus() + board.get("h3").getStatus() + " 3  |");
    
    System.out.println("|  4 " + board.get("a4").getStatus() + "" + board.get("b4").getStatus() + board.get("c4").getStatus() + board.get("d4").getStatus()
             + board.get("e4").getStatus() + board.get("f4").getStatus() + board.get("g4").getStatus() + board.get("h4").getStatus() + " 4  |");
    
    System.out.println("|  5 " + board.get("a5").getStatus() + "" + board.get("b5").getStatus() + board.get("c5").getStatus() + board.get("d5").getStatus()
             + board.get("e5").getStatus() + board.get("f5").getStatus() + board.get("g5").getStatus() + board.get("h5").getStatus() + " 5  |");
    
    System.out.println("|  6 " + board.get("a6").getStatus() + "" + board.get("b6").getStatus() + board.get("c6").getStatus() + board.get("d6").getStatus()
             + board.get("e6").getStatus() + board.get("f6").getStatus() + board.get("g6").getStatus() + board.get("h6").getStatus() + " 6  |");
    
    System.out.println("|  7 " + board.get("a7").getStatus() + "" + board.get("b7").getStatus() + board.get("c7").getStatus() + board.get("d7").getStatus()
             + board.get("e7").getStatus() + board.get("f7").getStatus() + board.get("g7").getStatus() + board.get("h7").getStatus() + " 7  |");
    
    System.out.println("|  8 " + board.get("a8").getStatus() + "" + board.get("b8").getStatus() + board.get("c8").getStatus() + board.get("d8").getStatus()
             + board.get("e8").getStatus() + board.get("f8").getStatus() + board.get("g8").getStatus() + board.get("h8").getStatus() + " 8  |");
    
    System.out.println("|    abcdefgh    |");
    System.out.println("+----------------+");
}

}

它基本上打印出:

想法是,当用户提供的输入等于HashMap的键之一时,该键的值将更改为方黑色或方白色(X或O),并且仅当用户输入的键尚未具有Square black或Square white的值.第一次检查工作正常,但我似乎无法让第二次检查工作.即使它已经是X或O,它也会始终覆盖该值.

The idea is that when the user gives an input that is equal to one of the keys of the HashMap the value of that key will change to either Square black or Square white (X or O) AND the value only changes if the users inputted key doesnt already have the value of Square black or Square white. The first check works fine but i cant seem to get the second check working. It always overwrites the value even if it already was X or O.

要执行上述检查,我需要以下代码:

To perfom the above checks i have the following code:

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();
    }
    

我尝试更改||到&&但没有结果.我也尝试过:

I tried changing the || to && but no results. i also tried:

if(board.board.get(move) == empty)  {
        
        board.board.replace(move, black);
        board.printBoard();
        }else {
            System.out.println("that spot is already taken");
        }

但是无论我输入什么内容,它总是返回else语句,即使move等于空,if语句也永远不会为真.

But then no matter what input i give it always returns the else statement and the if statement is never true even if move equals empty.

有人知道为什么第二个if语句不起作用吗?它给了我零错误.

Any idea why the second if statement doesnt work? it gives me zero erros.

推荐答案

存在两个问题:if将始终为true,因为空白与黑色不同,并且您需要获取电路板位置的状态并将其与"; empty",例如

There are two problems: the if will always be true because empty differs from both black and white and you need to get the board position's status and compare that to "empty", like

if (board.get(move).getStatus().equals("empty")) {
    //Code here...
}

这篇关于Java Hashmap值检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆