I am trying to make a program that outputs a triangle and the program then should asks the user to enter the size of a triangle to print on the screen. After getting this number, the program should then print a triangle to the screen by printing a series of lines consisting of asterisks. Each line having one more asterisk than the previous line, up to the number entered by the user. After reaching the number entered by the user, on the next line, print one less asterisk and continue by decreasing the number of asterisks by one for each successive line.

For example:

If the user enters 3, then your program should display this:

*
**
***
**
*

If the user enters 6, then your program should display this:

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

Code:

        System.out.println("How many asterisks do you want?");
        line = kbd.nextInt();
        printLine(line);

public static void printLine(int line) {
        char number = 32;
        for (int x = line; x > 0; x--)
        {
            System.out.print("*");

        }

With printLine being my made up method for getting the users input. I dont know how to get the stars to go out to the right instand of just going down.

Please help!!!

What do you mean by stars going down instead of to the right?? Can you please show the output of your code??

Im guessing base on your code that if user inputs 6, the output is:

*
*
*
*
*
*

But Im just guessing (",)

I am trying to make a program that outputs a triangle and the program then should asks the user to enter the size of a triangle to print on the screen. After getting this number, the program should then print a triangle to the screen by printing a series of lines consisting of asterisks. Each line having one more asterisk than the previous line, up to the number entered by the user. After reaching the number entered by the user, on the next line, print one less asterisk and continue by decreasing the number of asterisks by one for each successive line.

For example:

If the user enters 3, then your program should display this:

*
**
***
**
*

If the user enters 6, then your program should display this:

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

System.out.println("How many asterisks do you want?");
line = kbd.nextInt();
printLine(line);

public static void printLine(int line) {
char number = 32;
for (int x = line; x > 0; x--)
{
System.out.print("*");

}

With printLine being my made up method for getting the users input. I dont know how to get the stars to go out to the right instand of just going down.

Please help!!!

Honestly, i couldn't really understand your code. Maybe that's because i suck. But to get the stars to move to the right, you need another loop inside the first loop, so it'll be something like:

for (int i=0;i<line;i++){  //this loop will make it go down
        for (int j=0;j<i+1 ;j++){      //this loop print the stars to the right
            System.out.print("*");
        }
        System.out.print("\n"); 
    }

This is for the top part. as for the bottom, it's just the other way round which wouldn't be too hard to do if you understand how to do the front part.

You can also do it with a single loop by just adding '*' to a String variable.

String str = "";

for (int i = 0; i < line; i++) {
     str = str + '*';
     System.out.println(str);
     System.out.println("\n");
}

And the bottom part, as wilsonz91 said, is just another way around.

String str = "";

for (int i = 0; i < line; i++) {
     str = str + '*';
     System.out.println(str);
     System.out.println("\n");
}

your output is:
*


**


***


Your output is sort of correct and wrong. If he/she only wants 'enter' one, then yours would be incorrect as it is looping the 'str' line. Which i don't think is what he/she wants.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.