Java HashMap意外为“空”

时间:2018-09-19 04:56:46

标签: java

我正在为我的Java课程制作一个项目。我写了一个TicTacToe程序,但是我没有检查所有方法,只检查了put方法和sort方法。我有一个空的HashMap而不是1。

这就是控制台中的内容:

Input your column
1
Input your row
1

null(ticTacToe.toConsole();)

哪里有错误?

TicTacToe测试类:

package TicTac;

import java.util.Scanner;

public class TicTacToeTest {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        TicTacToe ticTacToe = new TicTacToe();
        System.out.println("Input your column");
        int column = scanner.nextInt();
        System.out.println("Input your row");
        int row = scanner.nextInt();
        ticTacToe.selection(column,row);
        ticTacToe.toConsole();
     }
}

主要的 TicTacToe 类:

package TicTac;

import java.util.ArrayList;
import java.util.HashMap;

public class TicTacToe {
     HashMap<Integer, String> board = new HashMap<>();
     public void selection(int column, int row) {
         String xOrO = "x";
         if (column == 1 && row == 1) {
         board.put(1,xOrO);
        } else if (column == 1 && row == 2) {
            board.put(2,xOrO);
        } else if (column == 1 && row == 3) {
            board.put(3,xOrO);
        } else if (column == 2 && row == 1) {
            board.put(4,xOrO);
        } else if (column == 2 && row == 2) {
            board.put(5,xOrO);
        } else if (column == 2 && row == 3) {
            board.put(6,xOrO);
        } else if (column == 3 && row == 1) {
            board.put(7,xOrO);
        } else if (column == 3 && row == 2) {
            board.put(8, xOrO);
        } else if (column == 3 && row == 3) {
            board.put(9,xOrO);
        }
         else {
            System.out.println("Wrong data");
        }
    }

     public void checker() {
        for (int i = 1; i < 2; i++) {
             if (board.get(1).equals(board.get(2)) && 
 board.get(2).equals(board.get(3))){
            System.out.println(board.get(1) + " player won!");
        } else if (board.get(4).equals(board.get(5)) && board.get(5).equals(board.get(6))){
            System.out.println(board.get(4) + " player won!");
        } else if (board.get(7).equals(board.get(8)) && board.get(8).equals(board.get(9))) {
            System.out.println(board.get(7) + " player won!");
        } else if (board.get(1).equals(board.get(4)) && board.get(4).equals(board.get(7))) {
            System.out.println(board.get(1) + " player won!");
        } else if (board.get(2).equals(board.get(5)) && board.get(5).equals(board.get(8))) {
            System.out.println(board.get(2) + " player won!");
        } else if (board.get(3).equals(board.get(6)) && board.get(6).equals(board.get(9))) {
            System.out.println(board.get(3) + " player won!");
        } else if (board.get(1).equals(board.get(5)) && board.get(5).equals(board.get(8))) {
            System.out.println(board.get(1) + " player won!");
        } else if (board.get(3).equals(board.get(5)) && board.get(5).equals(board.get(7))) {
            System.out.println(board.get(3) + " player won!");
        }
    }
}

public void toConsole(){
    for (int i = 0; i < board.size(); i++) {
        System.out.println(board.get(i));
    }
}

}

2 个答案:

答案 0 :(得分:1)

您的toConsole方法逻辑不正确。您试图从0的{​​{1}}键处获取元素,而HashMap的键HashMap处有对象。

更改为Console方法,如下所示:

1

请访问HashMap docs

阅读有关HashMap实现的一些基础知识

答案 1 :(得分:0)

在您的toConsole()函数中,您有从索引0开始的循环。那时,电路板的大小为1。因此,循环将打印出board.get(0)的结果并结束。