Hey everyone! I have an assigment due next Monday, and in this assignment I must create an ASCII triangle in Java. I've managed to do that, but for extra credit I have to do something like this:
Here is the code I have so far:
/********************************************************************** * Program: Triangles * Author: * * Programming Assignment #2 * Date: * * This program prompts the user to input the length of the side of an * equilateral triangle that is larger than 2. An equilateral triangle * will then be created using the ASCII character "*" (ASCII graphics) * * Input: * Side of the equilateral triangle * * Output: * An equilateral triangle made in ASCII graphics **********************************************************************/ import java.io.*; import java.util.*; public class Triangle { public static void main(String[] args) { //Variable declarations - i is the main LCV (Loop Control Variable) int triangleside = 0, triangleheight = 0, i, j, side; String intriangleside = null, option = null; boolean Start = true; //Prepares input Console console = System.console(); //Welcomes the user to the program System.out.println("Welcome to the ASCII triangle drawing program!"); //Loop that controls whether the main part of the program should start or not while(Start==true) { try { intriangleside = console.readLine("Enter the length of the triangle's side: "); triangleside = Integer.parseInt(intriangleside); side = triangleside; //Tells the user that a number greater than or equal to two is required if one is input if (triangleside<2) { System.out.println("Sorry, please choose a number greater than or equal to 2."); } //Draws the triangle if the value is 2 or above else { System.out.println("Here is your triangle: "); System.out.println(); //Loop that controls how many lines are made for(i=triangleside; i>0; i--) { //Loop that controls how many spaces are in each line for(triangleheight=i; triangleheight<side; triangleheight++) { System.out.print(" "); } //Loop that controls how many stars are in each line for(triangleside=0; triangleside<i; triangleside++) { System.out.print("* "); } System.out.println(); } } Start=false; option = console.readLine("Would you like to make another triangle? Please type either 'Yes' or 'No': "); if (option.equals("Yes")) { Start=true; } else if (option.equals("No")) { System.out.println("Goodbye!"); System.exit(0); } } /*Finds if an inappropriate response (Ex: Typing in 1 when prompted for a yes or no answer) is given *and closes the program if one isn't*/ catch(Throwable e) { System.out.println("Please type in an appropriate response next time. Goodbye!"); System.exit(0); } } } }
Apparently I have to make a larger triangle out of the smaller ones, but I'm not quite sure how to approach it. Any help would be greatly appreciated!