I am trying to figure out how to print a triangle in the shape of this depending on the user input. If they input 4 the max height would be 4.
*
**
***
****
***
**
*
I have no idea how to print the triangle after it reaches its max height. So far my code is this. Please help!
import java.util.Scann er;
[code=java]
public class Main
{
public static void main(String[] args)
{
int n, line, asterik;
Scanner key = new Scanner(System. in);
System.out.prin tln("Please enter the size of the triangle you want");
n = key.nextInt();
for(line = 1; line <= n+(n-1); line++)
{
if(line <= n)
{
for(asterik = 1; asterik <= line; asterik++)
{
System.out.prin t("*");
}
}
else
{
for(asterik = n; asterik <= line && (asterik != 0); asterik--)
{
System.out.prin t("*");
}
}
System.out.prin tln(" ");
}
}
}[/code]
*
**
***
****
***
**
*
I have no idea how to print the triangle after it reaches its max height. So far my code is this. Please help!
import java.util.Scann er;
[code=java]
public class Main
{
public static void main(String[] args)
{
int n, line, asterik;
Scanner key = new Scanner(System. in);
System.out.prin tln("Please enter the size of the triangle you want");
n = key.nextInt();
for(line = 1; line <= n+(n-1); line++)
{
if(line <= n)
{
for(asterik = 1; asterik <= line; asterik++)
{
System.out.prin t("*");
}
}
else
{
for(asterik = n; asterik <= line && (asterik != 0); asterik--)
{
System.out.prin t("*");
}
}
System.out.prin tln(" ");
}
}
}[/code]
Comment