错误:public final static int=schlachtschiff的表达式开始非法



我正在尝试在字段上设置船只,但我的代码有些问题。上面写着

illegal start of expression

对于CCD_ 1。

我想防止无效输入。我使用了一些while循环,但我不确定这是否涵盖了它。

但首先我需要让程序运行。

public class setship{
static char[][] attacker = {
    { '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    { '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    { '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    { '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    { '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    { '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    { '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    { '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    { '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    { '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' }
};
public static void print(char[][] grid) {
    System.out.println(" 0123456789");
    String[] myArray = new String[]{"A", "B", "C", "D", "E",
                                    "F", "G", "H", "I", "J"};
    for (int i = 0; i < myArray.length; i++) {
        System.out.print(myArray[i]);
        for (int j=0; j<10; j+=1) {
            System.out.print(grid[i][j]);
            if(j == 9) {
                System.out.println();
            }
        }
        System.out.println();
    }
}
public static void main(String[] args){
    Scanner s = new Scanner(System.in);
    System.out.println("Where do you want to place your ship?");
    int row, col;
    public final static int  schlachtschiff = 1 ;
    //public final static in = 2;
    //public final static in  Zerstoerer = 3;
    //public final static in  Uboote = 4;
    while(col<0 & col>10 && row<0 & row>10){
        System.out.print("Try again.");
        row = s.nextInt();
        col = s.nextInt();
        while (attacker[row][col]= '#'){
             System.out.print("Try again.Already an ship there.");
             row = s.nextInt();
             col = s.nextInt();
        }
    }
    for(schlachtschiff=1; schlachtschiff != 0; schlachtschiff++){      
        System.out.println("Col: ");
        col = s.nextInt();
        System.out.println("row: ");
        row  = s.nextInt();
        String position;
        System.out.println("Position (l),(r),(o), (u): ");                                      
        position = s.next();
          if(position.equals("l")){
            for(int i =0; i<5; i++){
                attacker[row-i][col] = '#';
            }
        }
        else if(position.equals("r")){
            for(int i =0; i<5; i++){
                attacker[row+i][col] = '#';
            }
        }
        else if(position.equals("o")){
            for(int i =0; i<5; i++){
                attacker[row][col+i] = '#';
            }
        }
        else if(position.equals("u")) {
            for(int i =0; i<5; i++){
                attacker[row][col-i] = '#' ;
            }
        }
        else{
            return;
        }
        print(attacker);
    }
}

不能在方法内部执行此操作。它需要是类级别的

public final static int  schlachtschiff = 1 ;
//public final static in = 2;
//public final static in  Zerstoerer = 3;
//public final static in  Uboote = 4;

或者你可以做

int  schlachtschiff = 1;

如果需要在方法中。

 while (attacker[row][col]= '#')

这不是你比较的方式

 while (attacker[row][col] == '#')

最新更新