似乎无法在数组中正确打印字符

时间:2019-04-19 22:26:00

标签: java arrays

我正在尝试根据用户输入使用5x5或7x7或9x9或11x11数组方法打印由'*'构成的金字塔。

我的代码现在打印一半金字塔,似乎无法得到另一半。帮助将不胜感激。

public static char[][] pointUp (char asterik, int length){
        char [] [] pointUp = new char [length] [length];
        for (int i = 0; i < length; i++){
            for (int j = 0; j < length; j++) {
                pointUp[i][j] = ' ';
            }
        }

        int end = length; 
        int j = 0;
        for (int column = 0; column < end; column++) {
            for (int row = j; row <= length/2; row++) {
                pointUp[row][column] = asterik;
            }
            j++;
            end++;

        }
        return pointUp;
    }

预期用于5x5代码:

    *   
  * * *
* * * * *

(space intended for remaining 2 columns)

预期用于7x7代码,依此类推:

      *
    * * * 
  * * * * * 
* * * * * * *

(space intended for remaining 3 columns)



我得到7x7代码等等的东西(其他一半丢失了):

 *            
 * *          
 * * *        
 * * * *   

(space intended for remaining 3 columns)


2 个答案:

答案 0 :(得分:0)

这是您想要的吗?

class test {
    public static void main(String[] args) {
        testPointUp(21);
    }

    public static char[][] pointUp(char asterisk, int length) {
        // make sure length is odd, other wise you can't do this
        // I encourage to make your own Exception class for clarity (e.g. NotOddException)
        if (length % 2 == 0) 
            throw new RuntimeException("Can't make a pyramide with an even lower row");

        // make the desired return value
        char[][] ret = new char[length][length];

        // if you devide the length parameter by two and avoid the .5, you
        // get the amount of empty columns in the first row, decrement this every
        // for every row, do this while emptyCols >= 0
        int emptyCols = (int) length/2;
        int row = 0;
        int desiredAsterisks = 1;
        while (emptyCols >= 0) {
            // first, add all empty cols
            for (int i = 0; i < emptyCols; i++) {
                ret[row][i] = ' ';
            }
            // then, add the desired amount of asterisks
            for (int i = 0; i < desiredAsterisks; i++) {
                ret[row][i + emptyCols] = asterisk;
            }
            // and of course, add the remaining empty cols
            for (int i = emptyCols + desiredAsterisks; i < length; i++) {
                ret[row][i] = ' ';
            }
            // change all necessary variables
            emptyCols--;
            desiredAsterisks += 2;
            row++;
        }
        return ret;
    }

    public static void testPointUp(int length) {
        char[][] test = pointUp('*', length);
        // I use a StringBuilder rather then a String because I want to
        // append a lot of characters to the variable I want to print out
        // in the end. Strings are immutable so every time you concatenate a String
        // a new variable is being made and given to the String variable, using a 
        // StringBuilder is much more efficient in concatenating (appending).
        StringBuilder sb = new StringBuilder("");
        for (int i = 0; i < length; i ++) {
            for (int j = 0; j < length; j++) {
                sb.append(test[i][j]);
            }
            sb.append("\n");
        }
        System.out.println(sb);
    }
}

答案 1 :(得分:0)

你去哪儿!

public static char[][] pointUp(char asterik, int length) {
    char[][] pointUp = new char[length][length];
    int halfLength = length / 2;
    for (int i = 0; i < halfLength + 1; i++) {
        int end = halfLength - i;
        for (int j = 0; j < end; j++) {
            pointUp[i][j] = ' ';
        }
        for (int k = 0; k < 2 * i + 1; k++)
            pointUp[i][k + end] = asterik;
    }

    return pointUp;
}

将模式分解成系列,然后尝试解决较小的系列并建立您的解决方案。