Alex
Alex

Reputation: 25

Inverted triangle in Java

I was trying to make an inverted triangle in java but I don't know what's wrong with my code

public void Tr1(int height, int begin, int pos, char ch){
    for (int i = 0;i < height;i++){
        for (int k = height;k > height - 1 - i;k--){
            System.out.print(" ");
        }
        for (int j = 0;j < pos;j++){
            System.out.print(" ");
        }
        for (int v = 0;v < i*2+begin;v++){
            System.out.print(ch);
        }
        System.out.println();
    }
}

The result is:

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

And I want this

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

Upvotes: 0

Views: 2131

Answers (5)

Mr. Polywhirl
Mr. Polywhirl

Reputation: 48610

You can draw an upside-down equilateral triangle using the method draw() below.

Of course, this is not a true equilateral triangle because although all sides have the same number of asterisks. Monospace characters have an average width-height ratio of 3:5.

* * *
 * *
  *

DrawTriangle.java

import java.util.*;

public class DrawTriangle {
    public static void main(String[] args) {
        System.out.println(draw("*", 10));
    }

    public static String draw(String symbol, int side) {
        StringBuilder sb = new StringBuilder();

        // Top line
        for (int i = side; i > 0; i--){
            sb.append(symbol + " ");
        }
        sb.append("\n");

        // Sides
        for (int i = side-2; i > 0; i--){
            // Left Side
            for (int j = 1; j < side-i; j++) {
                sb.append(" ");
            }
            sb.append(symbol);

            // Right Side
            for (int k = 1; k <= (i * 2)- 1 ; k++) {
                sb.append(" ");
            }
            sb.append(symbol).append("\n");
        }

        // Bottom point
        for (int i = side-1; i > 0; i--) {
            sb.append(" ");
        }
        return sb.append(symbol).append("\n").toString();
    }
}

Output

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

Upvotes: 0

Master Slave
Master Slave

Reputation: 28529

Many similar questions have been asked, one for example I'm having trouble making a diamond shape with loops

I posted a recursive answer, that can easily be adapted to your case

public class InvertedTriangle {
    static int iteration = 0;
    public static void printDiamond(int n) {
        int numberOfBlanks = n - iteration;
        int numberOfStars = iteration * 2 + 1;
        String blank = new String(new char[numberOfBlanks]).replace("\0", " ");
        String star = new String(new char[numberOfStars]).replace("\0", "*");
        String row = blank + star + blank;

        iteration++;
        if (iteration < n) {
            printDiamond(n);
        }
        // printing the rows backward
        System.out.println(row);
    }

    public static void main(String[] args) {
        printDiamond(5);
    }
}

This prints the

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

UPDATE after you question edit

public class InvertedTriangle {
    static int iteration = 0;

    public static void printDiamond(int n) {
        int numberOfBlanks = n - iteration;
        int numberOfInnerBlanks = iteration > 0 ? iteration * 2 - 1 : 0;
        String blank = new String(new char[numberOfBlanks]).replace("\0", " ");
        String innerBlank = new String(new char[numberOfInnerBlanks]).replace("\0", " ");
        String star = "*";
        String row = iteration == n - 1 ? blank + new String(new char[n * 2 - 1]).replace("\0", "*") + blank : iteration > 0 ? blank + star + innerBlank + star + blank : blank + star + blank;

        iteration++;
        if (iteration < n) {
            printDiamond(n);
        }
        // printing the rows backward
        System.out.println(row);
    }

    public static void main(String[] args) {
        printDiamond(10);
    }
}

This prints the

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

Upvotes: 2

drgPP
drgPP

Reputation: 936

A simple method draw() and call:

public class Triangle{

    public static void main(String[] args){

       Triangle.draw(5, "*");

        }

    public static void draw (int height, String st){
        for (int i=0; height>i; height--){
            System.out.println(new String(new char[height]).replace("\0", st));
        }
    }


    }

Result:

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

Upvotes: 0

almightyGOSU
almightyGOSU

Reputation: 3739

Here you go?

public static void main (String[] args)
{
    int height = 9; // Height of triangle
    char ch = '*';

    for(int currHeight = 0; currHeight <= height; currHeight++)
    {
        for(int space = 0; space < currHeight; space++)
        {
            System.out.print(" ");
        }

        int requiredStars = (height - currHeight) * 2 + 1;
        for(int stars = 0; stars < requiredStars; stars++)
        {
            System.out.print(ch);
        }
        System.out.println();
    }
}

Output:

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

You just have to manipulate the variables used in the for loops.
It is not easy trying to guess what type of triangle you need.


Use better variable naming conventions rather than j, k, v

Upvotes: 0

Joop Eggen
Joop Eggen

Reputation: 109557

The first loop can be rewritten, as k is not further used.

for (int k = height; k > height - 1 - i; k--) {
for (int k = 0; k > - 1 - i; k--) {
for (int k = 0; k < 1 + i; ++k) {

But you mean the inverse, decreasing the indentation:

for (int k = 0; k < height - 1 - i; ++k) {

were i maximally will be height - 1.

Upvotes: 0

Related Questions