改变矩形二维数组的值

时间:2012-12-04 20:30:23

标签: java arrays for-loop multidimensional-array

我正在创建一个非常基本的战舰计划,以获得乐趣和我 我的矩形二维数组中的值发生了变化。基本上,在这个二维阵列中放置了五个“船”。然后,用户输入一个整数以查看它们是否命中。我希望输出显示数组,其中猜测的数字表示为X表示未命中,0表示匹配。如果area[i][j]的值等于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(""); } } } } } 的值。我是java的新手,所以我想学习。任何帮助,将不胜感激。提前谢谢。

{{1}}

2 个答案:

答案 0 :(得分: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.
} 

充实您需要的所有方法,在需要时添加字段,这样可以更好地工作!

答案 1 :(得分:0)

这很简单,修改你的代码:

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索引的,因此您的船位与您设置网格的方式不完全一致。即区域[4] [1]不是“41”。你似乎正确地(在驱逐舰的情况下)为列而不是行执行此操作。如果船舶位于[4] [0]区域[4] [1],区域[4] [2],则if语句应检查31,32,33。事实上,你可能最好从00,01,02,...,97,98,99标记位置,以便索引对应于数字。