0

私は楽しみのために非常に初歩的な戦艦プログラムを作成していますが、長方形の 2 次元配列の値を変更する際に問題が発生しました。基本的に、この 2 次元配列には 5 つの「船」が配置されています。次に、ユーザーは整数を入力して、ヒットまたはミスを確認します。出力にはX、ミスの場合と0ヒットの場合で表される推測された数を含む配列を表示する必要がありました。の値がまたはarea[i][j]の値と等しいときに、 の値を変更するのに問題があります。私はJavaが初めてなので、学習しようとしています。どんな助けでも大歓迎です。前もって感謝します。t0X

import java.util.*;
public class battleship {

    //Rudimentary Battleship game
    public static void main(String[] args) {
        System.out.println(" Hello and welcome to a basic version of Battleship.");
        System.out.println(" The objective of this game is to sink all five ships located on the grid listed below.");
        System.out.println(" Follow the prompt located underneath the grid ");
        final int ROWS = 10;
        final int COLS = 10;
        int sum = 0;
        int [][] area = { {1,2,3,4,5,6,7,8,9,10},
                  {11,12,13,14,15,16,17,18,19,20},
                  {21,22,23,24,25,26,27,28,29,30},
                  {31,32,33,34,35,36,37,38,39,40},
                  {41,42,43,44,45,46,47,48,49,50},
                  {51,52,53,54,55,56,57,58,59,60},
                  {61,62,63,64,65,66,67,68,69,70},
                  {71,72,73,74,75,76,77,78,79,80},
                  {81,82,83,84,85,86,87,88,89,90},
                  {91,92,93,94,95,96,97,98,99,100} };

        for(int i=0; i < area.length; i++) {
            for (int j=0; j < area[i].length; j++) {


              if(area[i][j] < 10) 
                System.out.print("   "+(area[i][j])+" ");  
              else if(area[i][j] < 100) 
                System.out.print("  "+(area[i][j])+" ");
              else 
                System.out.print(" "+(area[i][j])+" ");
            }
          System.out.println("");
        }

    Scanner input = new Scanner(System.in);{
    System.out.println("Enter attack integer:");
    int t;
    while(true){
    t = input.nextInt();

    if ((t == 41) || (t == 42) || (t == 43)){
    System.out.println("Hit - Destroyer");}

    if ((t == 80) || (t == 90) || (t == 100)){
        System.out.println("Hit - Submarine");}


    if((t == 52) || (t == 62) || (t== 72) || (t == 82) || (t == 92)){
        System.out.println ("Hit - Aircraft Carrier");}

    if((t == 15) || (t == 16) || (t == 17) || (t == 18)){
        System.out.println ("Hit - Battleship");}

    if((t == 1) || (t == 2)){
        System.out.println ("Hit - PT Boat");}
    else{
        System.out.println ("Miss");
    }
    System.out.println("You have fired at:" + t);
    int w = 0;
    for(int i=0; i < area.length; i++) {
        for (int j=0; j < area[i].length; j++) {

            if (area[i][j] == t)

          if(area[i][j] < 10) 
            System.out.print("   "+(area[i][j])+" ");  
          else if(area[i][j] < 100) 
            System.out.print("  "+(area[i][j])+" ");
          else 
            System.out.print(" "+(area[i][j])+" ");
        }
      System.out.println("");
    }

        }
        }
    }
} 
4

2 に答える 2

2

オブジェクト指向を使用する方がはるかに優れています。開始するためのスケルトンを次に示します。

public class Cell {
  private boolean ship = false;
  private boolean hit = false;

  public boolean getHit() {
    return hit;
  }
  public void setHit(boolean hit) {
    this.hit = hit;
  }
}

それで

public class Board() {
  public static final int SIZE = 10;
  private Cell[][] board;

  public Board() {
    board  = new Cell[SIZE][SIZE];
    for(int i = 0; i < 10; i++)
      for(int j = 0; j < 10; j++)
        board[i][j] = new Cell();

  public boolean getHit(int x, int y) {
    if(x < 0 || x >= SIZE || y < 0 || y >= SIZE) throw new BattleshipException("You queried a cell that doesn't exist!");
    return board[x][y].getHit();
  }
  // etc etc.
} 

必要なすべてのメソッドを具体化し、必要なときにフィールドを追加すると、はるかにうまく機能します!

于 2012-12-04T20:43:39.783 に答える
1

これは簡単で、コードを変更します。

if ((t == 41) || (t == 42) || (t == 43)){
    System.out.println("Hit - Destroyer");
    area[i][j] = "X";
}
if ((t == 80) || (t == 90) || (t == 100)){
    System.out.println("Hit - Submarine");
    area[i][j] = "X";
}
if((t == 52) || (t == 62) || (t== 72) || (t == 82) || (t == 92)){
    System.out.println ("Hit - Aircraft Carrier");
    area[i][j] = "X";
}

if((t == 15) || (t == 16) || (t == 17) || (t == 18)){
    System.out.println ("Hit - Battleship");
    area[i][j] = "X";
}
if((t == 1) || (t == 2)){
    System.out.println ("Hit - PT Boat");
    area[i][j] = "X";
}
else{
    System.out.println ("Miss");
    area[i][j] = "O";
}

注意: 配列のインデックスは 0 であるため、船の位置がグリッドの設定方法と正確に一致しないことに注意してください。つまり、area[4][1] は "41" ではありません。行ではなく列に対して(駆逐艦の場合)これを正しく行っているようです。if ステートメントは、船がエリア [4][0]、エリア [4][1]、エリア [4][2] にあるかどうか、31、32、33 をチェックする必要があります。実際、インデックスが数字に対応するように、00、01、02、...、97、98、99 の位置にラベルを付けたほうがよい場合があります。

于 2012-12-04T20:46:37.007 に答える