Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
232 views
in Technique[技术] by (71.8m points)

java - I want to print this! (+Blank, and use character)

i made ms excel example:
i made ms excel example

Anyway, I made code. But this code is print only half. Why, why,!

for (int i = 1 ; i <= 5 ; i++) {
        for (int j = 1 ; j <= 9 ; j++) {
            if ( j < 10 - i ) {
                System.out.print(" "); 
            } else
                System.out.print("*");
        } System.out.println();
    }

Why printed only half.: (


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your condition is wrong, it should be:

for (int i = 1; i <= 5; i++) {
    for (int j = 1; j <= 9; j++) {
        if (j <= 5 - i || j >= 5 + i) {
            System.out.print(" ");
        } else {
            System.out.print("*");
        }
    }
    System.out.println();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...