方法以脱机方式打印数组元素,并且连字符乱序

时间:2019-04-18 04:28:14

标签: java arrays loops format output

因此,我试图打印出由用户输入创建的数组内容(大小必须为奇数,且在3到11之间),该内容是数组的列和行的两倍。用字符在某些地方做图案。除了格式,其他一切都很好。它可以正确打印,但打印位置不正确。字符关闭,并且由于某些原因连字号打印顺序错误。它们应该在数组前后打印。连字符的数量是正确的,只是我应该得到

-----------                             
 *    
   *      
     *    
       *  
         *
-----------

但是我得到了

*     
 *   
  *  
   * 
    *
-----------

                       -----------

我不知道为什么另一条连字符太远了,这是多么滑稽。这是代码

  public static void main (String [] args) {
    // instance variables - replace the example below with your own

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

    char star = '*';

    array2d = leftDiagonal(star, dimension);
    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( int row = 0; row < arrayParam.length; row++) {
        for (int column = 0; column < arrayParam.length; column++) {
            System.out.print(" " + arrayParam[row][column]);
        }
    }
    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) {
                System.out.print(starParam);
            } 
            else {
                System.out.print(' ');
            }
        }
        System.out.println();
    }
    return leftD;
}

更新:考虑到我被告知的事情后,我设法获得了正确的代码。在这里,再次感谢大家的帮助。

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

1 个答案:

答案 0 :(得分:1)

您的代码中有几个问题,我已修复并在代码中提及:

 import java.util.Scanner;
 public class PrintShape
 {
    public static void main (String [] args) 
    {
        int dimension = findDimension();
        char [] [] array2d = new char [dimension] [dimension];

        char star = '*';

        array2d = leftDiagonal(star, dimension);
        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) 
    {
        // i cant understand why are you printing so many hyphen "(arrayParam.length*2)+1"
        // so i left it on you 
        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 ");
        }

        //Problem: 
         // this "-" starts where the array printing end as you are not printing any newline ..
         // it starts printing hyphen on the same line.. that why you get the second row of "-" so far 

         //Fixed:
        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)
                                     {
                    // Probelm : you are just printing the "*"and no saving it in the array 
                    // thats why you are getting only blank spaces in the "print()"
                    System.out.print(starParam);
                                            leftD[i][j] = starParam;
                                      }
                else 
                    System.out.print(' ');
                // soution : save it in the array

            }
            System.out.println();
        }
        return leftD;
    }
}

如果遇到困难,请告诉我。