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

问题描述 投票:-1回答:2

我试图根据用户输入使用数组方法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)


java arrays
2个回答
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);
    }
}

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

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

© www.soinside.com 2019 - 2024. All rights reserved.