Java战舰程序没有正确打印板

时间:2017-05-02 14:37:23

标签: java arrays if-statement

我的java战舰程序出现问题而不是打印用户输入的坐标。当用户在棋盘上猜到一个位置时,它应该使用" X"来更新空间。如果它是一个打击,如果没有,那么董事会保持不变。但是当用户在我的程序上猜错时,它会打印除用户猜测的空间之外的所有内容。我相信我的其他声明,董事会更新是问题,但我所做的任何修改导致董事会不打印任何东西。

import java.util.Random;
import java.util.Scanner;



class Battleship {
     public static char randChar(){
         final String alphabet = "ABCDE";
         final int N = alphabet.length();
         char rand;
         Random r = new Random();
         rand = alphabet.charAt(r.nextInt(N));
         return rand;
    }

    public static void main (String[] args) throws java.lang.Exception {
        char[] letters = {' ', 'A', 'B', 'C', 'D', 'E'};
        int[] numbers ={1, 2, 3, 4, 5};
        int[][] ships = new int[7][2];
        char colGuess;
        int rowGuess;
        Boolean boardFlag=false;
        Scanner scan = new Scanner(System.in);
        //creates the board
        for (int i = 0 ; i <= 5 ; i++) {
            for (int j = 0 ; j <= 5 ; j++) {
                if (i == 0) {                          
                    System.out.print(letters[j] + " ");
                }
                else if (j == 0) {
                    System.out.print(numbers[i - 1]);
                }
                else {
                    System.out.print(letters[j] + "" + numbers[i-1]);
                }          
                System.out.print(" ");
            }
        System.out.println();
        }
        //assigns ships to random spots
        assignShips(ships);
        System.out.println("Enter your guess for the column:");
        colGuess = scan.next().charAt(0);
        //converts to uppercase
        colGuess = Character.toUpperCase(colGuess);
        System.out.println("Enter your guess for the row:");
        rowGuess = scan.nextInt();
        //shows player what they entered
        System.out.println("you entered: " + (char) colGuess + rowGuess);
        //calls method to check for a hit
        fire(colGuess, rowGuess, ships);
        boardFlag = fire(colGuess, rowGuess, ships);
        System.out.println(boardFlag);
        //updates the board
        for (int i = 0 ; i <= 5 ; i++) {
            for (int j = 0 ; j <= 5 ; j++) {
                if (i == 0) {
                    System.out.print(letters[j] + " ");
                }
                else if (j == 0) {
                    System.out.print(numbers[i - 1]);
                }
                else {
                    if(letters[j] == colGuess && numbers[i - 1] == rowGuess) {
                        if(boardFlag==true) {
                            System.out.print(" " + "X");
                        }
                    }
                    else {
                        System.out.print(letters[j] + "" + numbers[i - 1]);
                    }
                }          
                System.out.print(" ");
            }
            System.out.println();
        }
    }

    public static void assignShips(int[][] ships) {
        Random random = new Random();
        for(int ship = 0; ship < 7; ship++) {
            ships[ship][0] = randChar();
            ships[ship][1] = random.nextInt(5);
            //gives location of ships, for testing purposes
            System.out.print("Ship:" + (ship+1)+ " is located at"+(char)ships[ship][0]+ships[ship][1]+"\n");
        }
    }

    //checks user input to see if we have a hit
    public static Boolean fire(char colGuess, int rowGuess, int[][] ships) {
        Boolean hitFlag=false;
        for(int ship = 0; ship < ships.length; ship++){
            if(colGuess ==ships[ship][0] && rowGuess == ships[ship][1]){
                hitFlag=true;
            }
        }
        if(hitFlag == true) {
            System.out.println("we hit em at "+(char)colGuess+rowGuess+" chief!");
        }
        else { 
            System.out.println("sorry chief we missed em");
        }
        return hitFlag;
    }
}

3 个答案:

答案 0 :(得分:0)

当你错过的时候没有出现的原因是因为你的网告诉它打印任何东西。

if(letters[j] == colGuess && numbers[i-1] == rowGuess){
    if(boardFlag==true) {
        System.out.print(" "+"X");
    }
}

您只能处理用户输入的坐标。要解决此问题,您需要在if(boardFlag == true)之后使用else语句来处理打印(如果错过)。

答案 1 :(得分:0)

问题在于代码的更新板部分,因为如果猜到的位置没有被点击,您就不会做任何事情。

if(letters[j]==colGuess && numbers[i-1]==rowGuess && boardFlag) {
    System.out.print(" "+"X");
} else {
    System.out.print(letters[j]+""+numbers[i-1]);
}

完整更新部分:

//updates the board
for (int i = 0 ; i <= 5 ; i++){
    for (int j = 0 ; j <= 5 ; j++){
        if (i == 0) {
            System.out.print(letters[j]+" ");
        } else if (j == 0){
            System.out.print(numbers[i-1] );
        } else {
            if(letters[j]==colGuess && numbers[i-1]==rowGuess && boardFlag){
                System.out.print(" "+"X");
            } else {
                System.out.print(letters[j]+""+numbers[i-1]);
            }
        }          
        System.out.print(" ");
    }
    System.out.println();
}

答案 2 :(得分:0)

此块(旧版本):

sorry chief we missed em at D3      //D3 is a random possible set of coordinates

应该是(新版本):

sorry chief we missed em            //Old version

这将打印:

(char)colGuess+rowGuess

而不是:

CREATE TABLE Customer
(
    C_ID INT NOT NULL PRIMARY KEY,
    C_Name VARCHAR(255) NOT NULL,
    Phone char(10),
    CONSTRAINT valid_phone_number 
    CHECK (REGEXP_LIKE(p_number, '^0\d{9}|\d{10}$'))
);

所以你需要添加的是<tr> <td> ... <input type="radio" name="x_1" .../> </td> <td> ... <input type="radio" name="x_1" .../> </td> </tr> <tr> <td> ... <input type="radio" name="x_2" .../> </td> <td> ... <input type="radio" name="x_2" .../> </td> </tr> ,它会打印你之前猜测的坐标。

如果您有任何疑问,请发表评论!