Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 战列舰阵列检查_Java_Arrays - Fatal编程技术网

Java 战列舰阵列检查

Java 战列舰阵列检查,java,arrays,Java,Arrays,我正在制作一款战舰游戏,但在同一地点拍摄时遇到了问题。对于shot方法,我有一个单独的数组,对行保留shot[0],对列保留shot[1]。我试图创建一个2d数组来存储行的快照[0]和列的快照[1]的位置;然后使用双阵列,我可以检查已经命中的位置。问题是,我不确定是否可以将信号数组存储在双数组[row][col]中的位置 在处理这段代码一段时间后,我得到了2D数组来存储shot[0]和shot[1]的值。但我不知道我做得是否正确: public static void shoot(int

我正在制作一款战舰游戏,但在同一地点拍摄时遇到了问题。对于shot方法,我有一个单独的数组,对行保留shot[0],对列保留shot[1]。我试图创建一个2d数组来存储行的快照[0]和列的快照[1]的位置;然后使用双阵列,我可以检查已经命中的位置。问题是,我不确定是否可以将信号数组存储在双数组[row][col]中的位置

在处理这段代码一段时间后,我得到了2D数组来存储shot[0]和shot[1]的值。但我不知道我做得是否正确:

    public static void shoot(int[] shoot, int[][] ships){
    int[][] check = new int[6][6];
    Scanner input = new Scanner(System.in);

    System.out.print("Enter AI Row: ");
    shoot[0] = input.nextInt();

    System.out.print("Enter AI Column: ");
    shoot[1] = input.nextInt();

    while((shoot[0] <= 0 || shoot[1] <= 0) ||(shoot[0] == 0 && shoot[1] == 0) || (shoot[0] > 5 || shoot[1] > 5)){
      System.out.println("You must enter a location greater than 0 and NOT over 5! ");
      System.out.print("Enter Row: ");
      shoot[0] = input.nextInt();

      System.out.print("Enter Column: ");
      shoot[1] = input.nextInt();
    }    

    int temp1 = 0, temp2 = 0;
    for (int row = 0; row < 25; row++){
      for (int col = 0; col < 25; col++){
        if (row == shoot[0] && col == shoot[1])
        {
          check[row][0] = shoot[0];
          check[row][col] = shoot[1];
          temp1 = row;
          temp2 = col;
        }
      }
    }

    if (check[temp1][0] == ships[temp1][0] && check[temp1][temp2] == ships[temp1][temp2])
    {
      System.out.print("You have already entered that location!");
    }

    shoot[0]--;
    shoot[1]--;

  }

我不确定你到底在问什么,但我会尽力回答。不需要for循环来检查射击的每个位置,因为您已经有了坐标。我不确定您的ships数组的尺寸,但是您可以使用用户输入的坐标直接访问变量。你可以这样做:

int beenShot = 1; //keep in mind these are arbitrary values
int notShot = 0; //I don't know how you are tracking if a shot has been fired there or not
if (ships[shoot[0]][shoot[1]] == beenShot)
{
  System.out.println("You have already entered that location!")
}
另外,作为旁注

(shoot[0] == 0 && shoot[1] == 0)
不需要第一个条件的一部分,因为第一个条件已经说明两个条件都为零,因为您在检查时正在检查其中一个条件是否为0:

(shoot[0] <= 0 || shoot[1] <= 0) 
无论如何,希望这有帮助