Java - How to create an alternating triangular pyramid?

I am trying to create a triangular pyramid of alternating characters "*" and "o", with the number of lines based on user input. The expected result that I am trying to achieve if the user enters "6" for the number of lines is:

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

The code I wrote for this:

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 user input "6":

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

, -, , , , . , , , , .

+4
5

:

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*
+2

, .

:

  • n
  • "*"
  • n , :
    • " *" "*O*"

. () , , .

, ( ).


n:

  • , ' '
  • new String(new char[n]).replace('\0', ' ')

:

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

! -, "o" "*" s , for . , . if, , "i" for . - modulo:

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();
}

, . !

+1

, .
( , @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 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);
}
+1

.

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();
}
+1

Source: https://habr.com/ru/post/1655672/


All Articles