ccbadger
ccbadger

Reputation: 123

Java - How to create alternating triangle pyramid?

I am trying to create a triangle pyramid of alternating "*" and "o" characters, with the number of rows being based on user input. The expected output I am trying to achieve, if the user inputs "6" for the number of rows, is:

      *
     *o*
    *o*o*
   *o*o*o*
  *o*o*o*o*
 *o*o*o*o*o*

The code I have written to achieve this is:

String star = "*";
String circle = "o";
System.out.println("Please enter number of rows: ");
int rows = in.nextInt();
for (int i = 0; i < rows; i++){
    for (int j = 0; j < rows-i; j++){
        System.out.print(star);
    }
    for (int k = 0; k <= i; k++){
        System.out.print(circle);
    }
    System.out.println();
}

However, the output from my code does not match the pyramid above. The output of my code, with a user input of "6", is:

******o
*****oo
****ooo
***oooo
**ooooo
*oooooo

After spending the last three hours scouring both this website and others, I have still come up lost on how to alternate the characters, how to have the correct number of characters in each row, and how to format the pyramid as the expected output is. I don't know if my code is completely wrong, or if I am only missing a part to make it work correctly, but any advice or references is greatly appreciated.

Upvotes: 4

Views: 2272

Answers (6)

joelraj67
joelraj67

Reputation: 1

Ex: If rows=3

*##

**#

***

class Main{
public static void main(String x[]){
    Scanner scan=new Scanner(System.in);
    int rows=scan.nextInt();
    for(int i=1;i<rows;i++){
        for (int j=0;j<i;j++)
        {
        System.out.print("*");
        }
    for (int j=rows;j>i;j--)
        {
        System.out.print("#");
        }
    System.out.println();
    }
}
}

See if that works. Thanks!

Upvotes: 0

Bohemian
Bohemian

Reputation: 425198

You could approach it another, far simpler, way.

In pseudo code:

  • create a String of n spaces
  • add "*" to it
  • loop n times, each iteration of the loop:
    • print it
    • replace " *" with "*O*"

This recognises a simple way to create the first line, and a simple way to create the next line from the previous line. Each replacement will match only the last (leading) space and the first star, replacing the space with a star, the star with an O and adding a star.

Usually the best way to solve a hard problem is to look at it in a way that makes it a simple problem (or a collection of simple problems).


A couple of ways to create a String of n spaces:

  • A loop that adds ' ' each iteration
  • new String(new char[n]).replace('\0', ' ')

How to replace certain characters of a String with other characters:

str = str.replace(" *", "*O*");

Upvotes: 5

user4910279
user4910279

Reputation:

Try this.

Scanner in = new Scanner(System.in);
System.out.println("Please enter number of rows: ");
int rows = in.nextInt();
for (int i = 0; i < rows; ++i) {
    System.out.printf("%" + (rows - i) + "s", "*");
    for (int j = 0; j < i; ++j)
        System.out.print("o*");
    System.out.println();
}

Upvotes: 1

Marcel
Marcel

Reputation: 3258

This method will work fine:

public void printPyramid (int input) {
    for (int row = 1; row <= input; row++) {
        for (int whitespace = input - 1; whitespace >= row; whitespace--) {
            System.out.print(" ");
        }
        System.out.print("*");
        for (int circle = 1; circle < row; circle++) {
            System.out.print("o*");
        }
        System.out.println();
    }
}
                         *
                        *o*
                       *o*o*
                      *o*o*o*
                     *o*o*o*o*
                    *o*o*o*o*o*
                   *o*o*o*o*o*o*
                  *o*o*o*o*o*o*o*
                 *o*o*o*o*o*o*o*o*
                *o*o*o*o*o*o*o*o*o*
               *o*o*o*o*o*o*o*o*o*o*
              *o*o*o*o*o*o*o*o*o*o*o*
             *o*o*o*o*o*o*o*o*o*o*o*o*
            *o*o*o*o*o*o*o*o*o*o*o*o*o*
           *o*o*o*o*o*o*o*o*o*o*o*o*o*o*
          *o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*
         *o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*
        *o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*
       *o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*
      *o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*
     *o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*

Upvotes: 2

toddeTV
toddeTV

Reputation: 1517

Here is a solution, easy to understand and friendly for beginners.
(If you want to go more advanced, look at the solution from @Bohemian♦ )

String star = "*";
String circle = "o";
System.out.println("Please enter number of rows: ");
int rows = in.nextInt();
for (int i = 0; i < rows; i++){
    // How many "o" do we need?
    int count_circle = i;

    // How many * do we need?
    int count_star = count_circle + 1;

    // Let's create the string with o and *
    String output = "";
    for(int j = 0; j < (count_circle + count_star); j++){
        if(j % 2 == 0) // if it is odd
            output = output + star;
        else // if it is even
            output = output + circle;
    }

    // Do we need spaces at the beginning?
    String spaces = "";
    for(int j = 0; j < rows - i - 1; j++){
        spaces = spaces + " ";
    }

    // Final output
    output = spaces + output;
    System.out.println(output);
}

Upvotes: 1

Dave Fyre
Dave Fyre

Reputation: 60

Welcome to Stack Overflow! First, the "o"s and "*"s are not alternating because the for loops execute until completion. This means the stars and circles will print out separately. For this application you only need one for loop and two if statements based on whether the "i" in the for loop is odd or even. An easy way to do this is with the modulo function :

String star = "*";
String circle = "o";
System.out.println("Please enter number of rows: ");
int rows = in.nextInt();
for (int i = 0; i < rows; i++){
    if ((i % 2) == 0)
    {
        System.out.print(circle);
    }
    else
    {
        system.out.print(star);
    }
 System.out.println();
}

See if that works. Thanks!

Upvotes: 1

Related Questions