import java.util.Scanner;
import java.util.Random;
public class DrawTriangle
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println ("Do you want to see a triangle? Insert y to continue");
String input = scan.next();
boolean cont = false;
if ((input.equals("y")))
{
cont = true;
double a = (40);
double b = (30);
double height = (Math.random() * (b - a + 1) + a);
for (int x = 1; x <= height; x++)
{
for (int y = 0; y < height - x; y++)
{
System.out.print(" ");
}
for (int y = 0; y < x; y++)
{
System.out.print("x ");
}
System.out.println();
}
}
else
{
cont = false;
System.out.println();
System.out.println ("Program ended");
}
}
}
I need the program to draw a triangle when a user enters ‘y’. This works, however I need the program to then ask the user to again enter an input if the user previously pressed ‘y’. Also I’m not sure if my random numbers are working, as everytime the triangle is the same size…
Change the if statement to while, ask user input again in the loop, and remove the else