似乎无法正确反向循环输出

时间:2019-04-18 09:16:36

标签: java arrays for-loop format output

因此,我尝试在某些位置用字符*填充数组以获取模式。数组的大小(行和列)相同,并由用户输入决定。必须为奇数,介于3和11之间,因此例如,如果将它们放在5中,则会形成5 x 5的数组。无论如何,我正在尝试反转

的输出
-----------
 *        
   *       
     *    
       *  
         *
----------- to get 

 -----------
          *        
        *       
      *    
    *  
  *
 -----------  but instead I get 
 -----------
  * * * * *
  * * * * *
  * * * * *
  * * * * *
  * * * * *
 -----------   I used 5 as the size example here in case that helps

问题是,至少我认为我的循环似乎工作不正常。这是代码

public static void main (String [] args) {

    int dimension = findDimension();
    char [] [] array2d = new char [dimension] [dimension];

    char star = '*';

    array2d = leftDiagonal(star, dimension);
    print(array2d);

    array2d = rightDiagonal(star, dimension);
    System.out.println();
    print(array2d);
}

public static int findDimension() {
    int dimension = 0;
    Scanner keybd = new Scanner(System.in); 
    do {
        System.out.print("Enter an odd integer between 3 and 11 please: ");
        dimension = keybd.nextInt();
    } while (dimension%2 == 0);
    return dimension;
}

public static void print(char [] [] arrayParam) {
    for (int hyphen = 0; hyphen < (arrayParam.length*2)+1; hyphen++) {
        System.out.print("-");
    }

    System.out.println();
    for(char[] row : arrayParam)
    {
        for(char c : row)
            System.out.print(" " + c);
        System.out.printf("\n");
    }

    for (int hyphen = 0; hyphen < (arrayParam.length*2)+1; hyphen++) {
        System.out.print("-");
    }
}

public static char [] [] leftDiagonal(char starParam, int dimenParam) {
    char [] [] leftD = new char [dimenParam] [dimenParam];
    for (int i = 0; i < dimenParam; i++){ 
        for (int j = 0; j < dimenParam; j++) {
            if (i == j) 
                leftD[i][j] = starParam;
            else 
                leftD[i][j] = ' ';
        }
    }
    return leftD;
}

我认为问题特别在这里,因为它决定了数组中保存了什么

public static char [] [] rightDiagonal(char starParam, int dimenParam) {
    char [] [] rightD = new char [dimenParam] [dimenParam];
     for (int i = 0; i < dimenParam; i++){ 
         for (int j = 0; j < dimenParam; j++) { 
                 rightD[i][j] = ' ';
// I fill all the element spaces with blanks first then put in the *
// If there's another way to do It I'd like to know
         }
     }

    for (int i = 0; i < dimenParam; i++){ 
        for (int j = rightD.length-1; j >= 0; j--) { 
                rightD[i][j] = starParam;
        }
    }
    return rightD;
}

3 个答案:

答案 0 :(得分:1)

您的rightDiagonal方法应类似于您的leftDiagonal方法。唯一的区别不是检查i==j,而是检查i==length-j

public static char [] [] rightDiagonal(char starParam, int dimenParam) {
    char [] [] rightD = new char [dimenParam] [dimenParam];
    for (int i = 0; i < dimenParam; i++){ 
        for (int j = 0; j < dimenParam; j++) {
            if (i == (dimenParam-1-j) )
                rightD[i][j] = starParam;
            else 
                rightD[i][j] = ' ';
        }
    }
    return rightD;
}

答案 1 :(得分:1)

将您的rightDiagonal方法更改为

public static char [] [] rightDiagonal(char starParam, int dimenParam) {
        char [] [] rightD = new char [dimenParam] [dimenParam];

        for (int i = 0; i < dimenParam; i++){
            for (int j = 0; j < dimenParam; j++) {
                rightD[i][j] = ' ';
// I fill all the element spaces with blanks first then put in the *
// If there's another way to do It I'd like to know
            }
        }

        for (int i = 0; i < dimenParam; i++){
            for (int j = rightD.length-1; j >= 0; j--) {
                if(i + j == rightD.length - 1) {
                    rightD[i][j] = starParam;
                } else {
                    rightD[i][j] = ' ';
                }
            }
        }
        return rightD;
    }

这里最重要的部分是条件:

if(i + j == rightD.length - 1) {
     rightD[i][j] = starParam;
}

答案 2 :(得分:1)

根据我的理解,您想在垂直中间拆分您的数组,然后在另一侧镜像它:

*    |
 *   |
  *  |
   * |
    *|

    *|
   * |
  *  |
 *   |
*    |

因此,您需要将数组的宽度除以2,然后从新宽度的*中减去索引

public static void swap_array() {
int x = your_size = 4; // 4 because thats a lot easier to understad than 5
String your_array[][] = new String[x][x];

for(int i = 0; i < your_array.length; i++) {
    your_array[i][i] = "*";
}

// Now you have your start array

int new_length = your_array.length / 2; // For the mid

for(int j = 0; j < your_array.length; j++) { // Because of the 2 Dimensions
   for(int k = 0; k < your_array.length; k++) {
        if(your_array.equals("*")) {
              your_array[j][k] = your_array[j][k - new_length];
        }
        break; // Because you only got one *, so we can cut the rest then
    }
 }

 // Output

 for(int l = 0; l < your_array.length; l++) {
     for(int m = 0; m < your_array.length; m++) {
         System.out.println(your_array[l][m]);
      }
  }
}

///尚未在IDE中测试过。它是由大脑动态完成的。不要复制粘贴