问题是我有几种方法可以在 Java 板上拍摄。您可以在代码中修改数组大小,它适用于任何维度。您可以选择许多不同的拍摄方向。例如,如果 a[x][9] ,您可以从 -4 拍摄到 4(不是 0)。问题是它适用于所有维度的所有方向,但不适用于限制(在本例中为 -4 和 4)。
我想问题一定出在常量上,但我不确定。我将代码粘贴在这里,以便您能够编译;)
public class Shooting{
public static char [][] b = new char [25][9];
public static char EMPTY = '-';
public static char SHOT = 'x';
public static Scanner sc = new Scanner(System.in);
public static int shootDirection;
public static int BOTTOM = b.length - 1;
public static int RIGHT_WALL = b[0].length - 1;
public static int LEFT_WALL = 0;
public static int TOP = 0;
public static int CENTER = ((RIGHT_WALL)/2);
public static int START = ((RIGHT_WALL)/2) + shootDirection;
public static void fill (char [][] b){
for (int x = 0; x < b.length; x++){
for (int y = 0; y < b[x].length;y++){
b [x][y] = EMPTY;
// b [10][y] = 'A';
}
}
}
public static void show (char [][] b){
for (int x = 0; x < b.length; x++){
System.out.println();
for (int y = 0; y < b[x].length; y++){
System.out.print(" "+b [x][y]+" ");
}
}
}
public static boolean isOut (int x,int y){
if(x < TOP)return true;
if(y > RIGHT_WALL)return true;
if(y < LEFT_WALL)return true;
return false;
}
static boolean emptyCell(int i, int j){
return b [i][j] == EMPTY;
}
public static void shootDude (char [][] a , int shootDirection){
START = ((RIGHT_WALL)/2) + shootDirection;
boolean shotRight = false;
if(shootDirection < 0)
shotRight = false;
else if(shootDirection > 0)
shotRight = true;
for(int i = a.length-1,j = START ;i >= 0;--i) {
if(!isOut(i,j) && !emptyCell(i,j)) break;
if(!isOut(i,j) && shotRight) {
a[i][j] = SHOT;
++j;
}
else {
a[i][j] = SHOT;
--j;
}
if(j <= LEFT_WALL)
shotRight = true;
if(j >= RIGHT_WALL)
shotRight = false;
}
}
public static void main (String[] args){
System.out.println("Introduce shot direction:");
// With b[x][9] you can shot from -4 to 4. The thing is that
//it doesnt work for -4 and 4, the edges. Can anyone solve it?
// Must be a silly thing ;)
int shootDirection = sc.nextInt();
fill (b);
shootDude(b,shootDirection);
show(b);
}
}
必须导入扫描仪;)