Как напечатать определенное значение в массиве с другими значениями изменить на «*»? - PullRequest
0 голосов
/ 25 февраля 2010

Например, в следующем макете:

.    8 * * *

.    * * * *

.    * 8 * *

.    * * * *

Все открытые карты обозначены *. Пары 8, которые направлены вверх, находятся в координатах (1,1) и (2,3).

Я сделал свой 2D-массив, и теперь я застрял в печати *. Я только что напечатал * * * *. я понятия не имею, как напечатать 8 внутри массива.

Любая помощь?

public static void Shuffles(){

    int[][]a = new int[4][4];
    for (int i =0; i<4;i++){
        for (int j=0;j<4;j++){

            System.out.print("*");
            System.out.print(" ");
        }
        System.out.println("");
        System.out.println("");
    }


}

public static void main (String [] args) {

    List<Integer> randoms = new ArrayList<Integer>();
    Random randomizer = new Random();
    int [][] memory = new int[4][4];

    for(int i = 0; i < 8; ) {
      int r = randomizer.nextInt(8)+1;
      if(!randoms.contains(r)) {
        randoms.add(r);
        ++i;
      }
    }

    List<Integer> clonedList = new ArrayList<Integer>();
    clonedList.addAll(randoms);
    Collections.shuffle(clonedList);
    randoms.addAll(clonedList);

    for(int i=0; i < 4; i++){
            memory[0][i] = randoms.get(i);
            memory[1][i] = randoms.get(i+4);
            memory[2][i] = randoms.get(i+8);
            memory[3][i] = randoms.get(i+12);
    }

    for (int i =0; i<4;i++){
        for (int j=0;j<4;j++){

            System.out.print(memory[i][j]);
            System.out.print(" ");
        }
        System.out.println("");
        System.out.println("");
    }


    int x1,y1,x2,y2;
    Scanner input = new Scanner(System.in);

    System.out.print("Please enter the coordinate [x1] : ");
    x1 = input.nextInt();
    while((x1<1) || (x1>4)){
        System.out.print("Invalid coordinate!! Re-enter [x1] : ");
        x1 = input.nextInt();
    }

    System.out.print("Please enter the coordinate [y1] : ");
    y1 = input.nextInt();
    while((y1<1) || (y1>4)){
        System.out.print("Invalid coordinate!! Re-enter [y1] : ");
        y1 = input.nextInt();
    }

    System.out.print("Please enter the coordinate [x2] : ");
    x2 = input.nextInt();
    while((x2<1) || (x2>4)){
        System.out.print("Invalid coordinate!! Re-enter [x2] : ");
        x2 = input.nextInt();
    }

    System.out.print("Please enter the coordinate [y2] : ");
    y2 = input.nextInt();
    while((y2<1) || (y2>4)){
        System.out.print("Invalid coordinate!! Re-enter [y2] : ");
        y2 = input.nextInt();
    }

    x1=x1-1;
    y1=y1-1;
    x2=x2-1;
    y2=y2-1;

    if(memory[x1][y1] != memory[x2][y2]){
        Shuffles();
    }

} }

это то, что я сделал до сих пор ...

Ответы [ 3 ]

1 голос
/ 25 февраля 2010

Вы ищете это?

<code>
// k is the number to display
//a[][] is the array</p>

<p>public static void Shuffles(int k, int[][]a)
{
    for (int i =0; i<4;i++)
    {
        for (int j=0;j<4;j++)
        {
            if(a[i][j] == k)
            {
                System.out.print(a[i][j]);
            }
            else
            {
                System.out.print("*");
            }
            System.out.print(" ");
        }
        System.out.println("");
        System.out.println("");
    }
}
0 голосов
/ 25 февраля 2010

Вы ищете вложенный цикл?

for (int y = 0; y < 8; y++)
{
    for (int x = 0; x < 8; x++)
    {
        // Use 'x' and 'y' here to index your 2-D 8x8 array
        system.out.println( ... );
    }
}
0 голосов
/ 25 февраля 2010

Печать позиций по одному. Для каждого из них, проверьте, если карта лицевой стороной вверх, если это так, то напечатайте значение, иначе напечатайте звездочку. System.out.print() позволит вам печатать без новой строки.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...