简体   繁体   中英

Java printing patterns using for loops

I need to get the following pattern 在此处输入图片说明

have developed following code.

    public static void main(String[] args) {
        int number = 9;
        for(int i=0;i<9;i++){
            for(int j=0;j<18;j++){
                if(number==6)
                    continue;
                System.out.print(number);
            }
            if(number != 6)
            System.out.println();
            number--;
        }
    }

But I cant think about the logic to get the curved part of the pattern. Can Anyone give an opinion?

if (j < number || j >= 18 - number)
    System.out.print(number);
else
    System.out.print(" ");

You could try this:

public class CurveOutput {

    public static void main(String args[]) {
        int startNumber = 9;

        for (int currentNum = startNumber; currentNum >= 0; currentNum--) {
            StringBuilder line = new StringBuilder();
            for (int i = 0; i < currentNum; i++) {
                line.append(currentNum);
            }
            for (int i = 0; i < startNumber - currentNum; i++) {
                line.append(" ");
            }
            System.out.println(line.toString() + line.reverse().toString());
        }
    }
}

this snippet produce:

999999999999999999
88888888  88888888
7777777    7777777
666666      666666
55555        55555
4444          4444
333            333
22              22
1                1

From my perspective it is really about programming on a correct level of abstraction .

The requirement is not to put as many spaces in the start or at the end, but rather to align the numbers to left or right. If there would be such functionality, it would be better and most likely more readable. And there is such:

public static void main(String args[]) {
    int startNumber = 9;

    for (int i = startNumber; i > 0; i--) {
        String numberToPrint = Strings.repeat("" + i, i); // from Google Guava

        String leftHalf  =  String.format("%-" + startNumber + "s", numberToPrint);
        String rightHalf =  String.format("%"  + startNumber + "s", numberToPrint);

        System.out.printf("%s%s%n", leftHalf, rightHalf);
    }
}

Try this...

int num=9,save=9;
        for(int i=1;i<10;i++)
        { 

            for(int j=1;j<=18&&num!=6;j++)
        { 
                int t=save-num;

                 if(((j<=(9-t)) || (j>(9+t))))
            System.out.print(num);

                else 
                    System.out.print(" ");
        }
            num=num-1;
        System.out.println("\n"); 
        } 
package q17;

public class Q17 {

    public static void main(String[] args) {

        int x = 9, y = 10;

        for (int i = x; i >= 1; i--) {
            if (i == 6) {
                x--;
                y++;
                continue;
            }
            for (int j = 1; j <= 18; j++) {

                if ((i != 9) && ((j >= x) && (j <= y))) {
                    System.out.print(" ");

                } else {
                    System.out.print(i);

                }

            }

            if (i != 9) {
                x--;
                y++;
            }
            System.out.println();
        }

    }

}

WORKING CODE: https://i.stack.imgur.com/NFNym.png

int num = 9, save = 9;
            for (int i = 1; i < 10; i++) {

                for (int j = 1; j <= 18 && num != 6; j++) {
                    int t = save - num;

                    if (((j <= (9 - t)) || (j > (9 + t)))) {
                        System.out.print(num);
                    } else {
                        System.out.print(" ");
                    }
                }
                num = num - 1;
                if (num != 6) {
                    System.out.println();
                }
            }

        }

OUTPUT:

999999999999999999
88888888  88888888
7777777    7777777
55555        55555
4444          4444
333            333
22              22
1                1

IMPROVED CODE of "Joby Wilson Mathews" (RE-POSTING AS SUGGESTION QUEUE IS FULL) NOTE: Earlier code was printing a blank line on line number 4, which is now fixed. Also provided a link to the output image.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM