我正在为《战舰》编写一个placeShips方法。 它在大多数情况下都可以工作...除非当我试图放置一艘船时,并且遇到错误情况(从板载,在该位置的另一艘船等)时,它将在直到意识到它不应该的空间。 我尝试使用级联的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++;
    }

  ask by user3552903 translate from so

本文未有回复,本站智能推荐: