userGuess[y, x]ship1[][]进行比较,但是当我输入用户猜测等于board[][]时,仅在与"!"匹配的索引处更新(ship1[2][0], ship1[2][1])

换句话说,我将updateBoard()输入int[] userGuess,updateBoard循环遍历int[][] ship1,并将userGuess[0]ship1[0][0]进行比较,并且将userGuess[1]ship1[0][1]...ship1[2][0]ship1[2][1]进行比较,以查看是否它们匹配,但是唯一得到的匹配是如果我输入的userGuess等于(ship1[2][0], ship1[2][1])的值。

有人看到我做错了吗?

package assignment_8;

import java.util.Scanner;

public class Battleship {

    private String[][] board = new String[8][8];
    private int[][] ship1 = new int[3][2];
    private int[][] ship2 = new int[4][2];
    private int counter;

    Battleship() {
    }

    public void makeBoard() {
        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[i].length; j++) {
                board[i][j] = "| ";
                board[i][7] = "| |";
            }
        }
    }
    public void makeShips() {
        int ship1Y = (int)Math.floor(Math.random() * 7);
        int ship1X = (int)Math.floor(Math.random() * 7);
        int ship2Y = (int)Math.floor(Math.random() * 7);
        int ship2X = (int)Math.floor(Math.random() * 7);
        int coinToss = (int)Math.floor(Math.random() * 2) + 1;
        if(coinToss == 1) {
            if(ship1X >= 5) {
                ship1X = 4;
            }
            for(int i = 0; i < ship1.length; i++) {
                ship1[i][0] = ship1Y;
                ship1[i][1] = ship1X + i;
            }
            if(ship2Y >= 4) {
                ship2Y = 3;
            }
            for(int i = 0; i < ship2.length; i++) {
                ship2[i][0] = ship2Y + i;
                ship2[i][1] = ship2X;
            }
        } else {
            if(ship1Y >= 5) {
                ship1Y = 4;
            }
            for(int i = 0; i < ship1.length; i++) {
                ship1[i][0] = ship1Y + i;
                ship1[i][1] = ship1X;
            }
            if(ship2X >= 4) {
                ship2X = 3;
            }
            for(int i = 0; i < ship2.length; i++) {
                ship2[i][0] = ship2Y;
                ship2[i][1] = ship2X + i;
            }
        }
    }
    public String[][] getBoard() {
        return board;
    }
    public int[][] getShip1() {
        return ship1;
    }
    public int[][] getShip2() {
        return ship2;
    }
    public int getCounter() {
        return counter;
    }

    public void updateBoard(int[] userInput) {
        for(int i = 0; i < ship1.length; i++) {
            if(ship1[i][0] == userInput[0] && ship1[i][1] == userInput[1] && board[userInput[0]][userInput[1]] == "| ") {
                counter++;
            } else if (ship1[i][0] == userInput[0] && ship1[i][1] == userInput[1] && board[userInput[0]][userInput[1]] == "| |") {
                counter++;
            }
            if(ship1[i][0] == userInput[0] && ship1[i][1] == userInput[1]) {
                board[userInput[0]][userInput[1]] = "|!";
                if(userInput[1] == 7) {
                    board[userInput[0]][userInput[1]] = "|!|";
                }
            } else {
                board[userInput[0]][userInput[1]] = "|X";
                if(userInput[1] == 7) {
                    board[userInput[0]][userInput[1]] = "|X|";
                }
            }
        }
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Battleship bs = new Battleship();
        bs.makeBoard();
        bs.makeShips();
        int[][] ship1 = bs.getShip1();
        //int[][] ship2 = bs.getShip2();
        for(int i = 0; i < ship1.length; i++) {
            System.out.println("[" + ship1[i][0] + "][" + ship1[i][1] + "]");
        }
        System.out.println();
        /*for(int i = 0; i < ship2.length; i++) {
            System.out.println("[" + ship2[i][0] + "][" + ship2[i][1] + "]");
        }*/
        System.out.println();
        int[] userGuess = new int[2];
        int counter = 0;
        String[][] board = bs.getBoard();
        do {
            for(int i = 0; i < board.length; i++) {
                for(int j = 0; j < board[i].length; j++) {
                    System.out.print(board[i][j]);
                }
                System.out.println();
            }
            counter = bs.getCounter();
            System.out.println("Counter = " + counter);
            System.out.print("Enter Y coordinate (0 - 7): ");
            userGuess[0] = sc.nextInt();
            System.out.print("Enter X coordinate(0 - 7): ");
            userGuess[1] = sc.nextInt();
            bs.updateBoard(userGuess);
            System.out.println();
        } while(counter < 8);
    }
}

最佳答案

问题在于以下条件检查:

if(ship1[i][0] == userInput[0] && ship1[i][1] == userInput[1]) {
    board[userInput[0]][userInput[1]] = "|!";
    if(userInput[1] == 7) {
        board[userInput[0]][userInput[1]] = "|!|";
    }
} else {
    board[userInput[0]][userInput[1]] = "|X";
    if(userInput[1] == 7) {
        board[userInput[0]][userInput[1]] = "|X|";
    }
}


如果用户提供与ship1[0][0]ship[0][1]匹配的坐标,则会为其分配!,但是循环将继续,接下来的两个ship1数组值将不匹配,从而将其重置为X

解决方法是在找到匹配项时将break移出循环。

if(ship1[i][0] == userInput[0] && ship1[i][1] == userInput[1]) {
    board[userInput[0]][userInput[1]] = "|!";
    if(userInput[1] == 7) {
        board[userInput[0]][userInput[1]] = "|!|";
    }
    break; // that's it!
}

关于java - 尝试制造战舰,但String [] []板无法正确更新,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59312243/

10-11 20:14