How to print a new line after each printing call

Viewed 928

I want to get as many stars i give in the function with a line break. But I am not able to get them with line break.

public class prac11 {

    public static void main(String[] args) {
        //printStars(1);
        printStars(2);
        printStars(3);
    }

    public static void printStars(int x) {
        int i=1;
        while(i<=x) {
            System.out.print("*");
            i++;
        }
    }
}

My output

Desired Output

2 Answers

You have to add a println statement to put the linebreak after your loop:

public class prac11 {

    public static void main(String[] args) {
        printStars(5);
        printStars(3);
        printStars(9);
    }

    public static void printStars(int x) {
        int i=1;
        while(i<=x) {
            System.out.print("*");
            i++;
        }
        System.out.println(); // this will produce a linebreak
    }
}

Output:

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

Add System.out.println() after the while loop, to get the linebreak.

Related