如何在遇到错误情况时阻止此循环在数组中打印某些东西?

时间:2014-04-20 00:32:09

标签: java arrays for-loop error-checking

我为游戏Battleship编写了placeShips方法。它大部分都有效......除非我试图放置一艘船,遇到错误情况(离开船上,另一艘船在该地点等),它将打印船舶和� #39;空间中的字符代码直到它意识到它不应该。我试图使用级联if语句,我不知道如何解决这个问题。这是代码的一小部分。有五种不同的船只,但这是仅将第一个放置在10x10阵列中的代码,所有字符都初始化为〜。

int count=1;
while(count<=5){
    start: {
    if(count==1){
    System.out.println("What row and column would you like to place your aircraft carrier?");
    System.out.println("Row?");
    int row1 = IO.readInt();
                    if(row1>9 || row1<0){
                    IO.reportBadInput();
                    break start;
                    }
    System.out.println("Column?");
    int column1 = IO.readInt();
                    if(column1>9 || column1<0){
                    IO.reportBadInput();
                    break start;
                    }

    System.out.println("How would you like to orient your aircraft carrier? 1. Down, 2. Up, 3. Left, 4. Right");
    int orientation = IO.readInt();
    if(orientation==1){ //oriented down

        for(int z=0; z<5; z++){ //places the aircraft carrier, DOWN
        if((row1+z)>=board.length){
            System.out.println("This will go off the board. You can't place your ship here! Try again.");
            IO.reportBadInput();
            break start;
        }

        else if(board[row1+z][column1]!='~'){
            System.out.println("Oops! You can't place your ship there. Ship already in this location. Try again.");
            IO.reportBadInput();
            break start;
        } //checks if there's anything else already in that space

        else{
            board[row1 + z][column1]= 'A';
        } 

        } 
    }
        if(orientation==2){ //oriented up

            for(int z=0; z<5; z++){ //places the aircraft carrier, UP ORIENTATION
            if((row1-z)<0){
                System.out.println("This will go off the board. You can't place your ship here! Try again.");
                IO.reportBadInput();
                break start;
            }
            else if(board[row1-z][column1]!='~'){
                System.out.println("Oops! You can't place your ship there. Ship already in this location. Try again.");
                IO.reportBadInput();
                break start;
            } //checks if there's anything else already in that space
            else{
                board[row1 - z][column1]= 'A';
            }

            }
        }

            if(orientation==3){

                for(int z=0; z<5; z++){ //places the aircraft carrier, LEFT ORIENTATION
                if((column1-z)<0){
                    System.out.println("This will go off the board. You can't place your ship here! Try again.");
                    IO.reportBadInput();
                    break start;
                }
                else if(board[row1][column1-z]!='~'){
                    System.out.println("Oops! You can't place your ship there. Ship already in this location. Try again.");
                    IO.reportBadInput();
                    break start;
                } //checks if there's anything else already in that space
                else{
                    board[row1][column1-z]= 'A'; 
                }

                }
            }


            if(orientation==4){

                for(int z=0; z<5; z++){ //places the aircraft carrier, RIGHT ORIENTATION
                if((column1+z)>=9){
                    System.out.println("This will go off the board. You can't place your ship here! Try again.");
                    IO.reportBadInput();
                    break start;
                }
                else if(board[row1][column1+z]!='~'){
                    System.out.println("Oops! You can't place your ship there. Ship already in this location. Try again.");
                    IO.reportBadInput();
                    break start;
                } //checks if there's anything else already in that space
                else{
                    board[row1][column1+z]= 'A';
                }

                }
}
    count++;
    }

0 个答案:

没有答案