How to make a triangle of asterisks in java

0

I want to get the following output:

n: 4

+

++

+++

++++

That is, I insert an "n" and I will get a kind of triangle in which the base corresponds to a number of asterisks that is requested by input. I made the following code but this only hits the number of lines and not of elements per line:

System .out . println("Indique um número inteiro positivo:");
    int n = scanner.nextInt();
    int triangulos = 0;
    int i ;
    for(i=0; triangulos < n ; triangulos++)
    {
        System.out.println("n:" + n);
        System.out.println("*");
    }
    
asked by anonymous 29.10.2017 / 00:30

1 answer

0
System.out.println("Indique um número inteiro positivo:");
int n = scanner.nextInt();
System.out.println("n: " + n);
for(int i = 0; i <= n ; i++) {
    String out = "";
    for (int j = 0; j < i; j++) {
        out.concat("*");
    }
    System.out.println(out);
}

In the first iteration, we go through the n

On the second we add "*" i times as iterates n

    
29.10.2017 / 01:00