【问题标题】:Can't seem to reverse loop output correctly似乎无法正确反转循环输出
【发布时间】:2019-04-18 09:16:35
【问题描述】:

所以我试图在某些地方用字符 * 填充数组以获取模式。数组的大小(行和列)相同,由用户输入决定。必须是奇数并且介于 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;
}

【问题讨论】:

  • 找对角线使用条件i + j = rightD.length-1

标签: java arrays for-loop format output


【解决方案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】:

    据我了解,您想在垂直中间拆分阵列,然后将其镜像到另一侧:

    *    |
     *   |
      *  |
       * |
        *|
    

    到

        *|
       * |
      *  |
     *   |
    *    |
    

    所以你需要将我们数组的宽度除以 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中测试过。它是在飞行中从大脑中完成的。不要复制粘贴

    【讨论】:

    • 免责声明:我有阅读障碍症,使用横杠阅读效果更好。骆驼案对我不起作用
    • 谢谢,我以前从未在 Java 中做过镜像,所以尝试一下很有趣。我仍然需要对其进行练习,但我一定会牢记这种方法以应对未来的问题。
    【解决方案3】:

    您的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;
    }
    

    【讨论】:

      猜你喜欢
      • 2022-12-14
      • 2020-09-15
      • 1970-01-01
      • 2023-03-29
      • 2021-10-29
      • 1970-01-01
      • 1970-01-01
      • 2015-10-14
      • 2018-01-14
      相关资源
      最近更新 更多