Triángulo, Noreste Aprende programación con ejercicios Java

Lección:

Tipos de datos básicos


Ejercicio:

Triángulo, Noreste 39


Objetivo:

Escriba un programa en java que solicite un ancho y muestre un triángulo como este:

Introduzca el ancho deseado: 5

*****
_****
__***
___**
____*


Código:

import java.util.*;
public class Main
{
	public static void main(String[] args)
	{
		int width, height;
		int row, column;
		int max;

		System.out.print("Enter the desired width: ");
		height = Integer.parseInt(new Scanner(System.in).nextLine());
		width = 0;
		max = height;

		for (row = 0; row < height; row++)
		{
			for (column = 0; column < width; column++)
			{
				System.out.print(" ");
			}

			for (int asterisks = 0; asterisks < max; asterisks++)
			{
				System.out.print("*");
			}

			System.out.println();
			width++;
			max--;
		}
	}
}