English 中文(简体)
如何在数组中打印特定值,将其他值更改为 *?
原标题:how to print particular value in array with other values change to * ?
  • 时间:2010-02-25 06:24:55
  •  标签:
  • java

例如,在以下规定中:

.    8 * * *

.    * * * *

.    * 8 * *

.    * * * *

所有背面朝下的牌都用*表示。背面朝上的一对8位于坐标(1,1)和(2,3)。

我已经完成了我的二维数组,现在我卡在了打印*上。我做的只打印* * * *。我不知道如何在数组中打印8

任何帮助吗? (Rèn hé bāng zhù ma?)

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("");
    }


}

公共静态真空主(String[] arg){

    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();
    }

} }

这是我到目前为止做的...

最佳回答

你在找这个吗?


// k is the number to display
//a[][] is the array

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(""); } }

问题回答

有时印刷职位。 每人检查卡面部,如果是的话,则打印面面值,否则就会印出 star。 <代码>Systemout.print()>将允许你在没有新线的情况下印刷。

你在找一个嵌套循环吗?

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( ... );
    }
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签