简体   繁体   中英

Pyramids won't print properly on the console

I am trying to get the simple pyramid program and my coding looks quite good and works as well and after compiling it won't produce the expected output!

Below is my code:

package DataTypes;

public class Pyramids {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        for(int x=0; x<=10; x++){
            for(int y=0; y<x; y++){
                System.out.println("*");

            }

            System.out.println();
        }

    }

}

Expected output is

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

but its returning the same output in the below manner;

*

*
*

*
*
*

*
*
*
*

*
*

*
*

*
*
*
*
*
*

*
*
*
*
*
*
*

*
*
*
*
*
*
*
*

*
*
*
*
*
*
*
*
*

*
*
*
*
*
*
*
*
*
*
System.out.println("*");

use instead

System.out.print("*");

You don't want to change the line every time you print * .

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