Question: (JAVA) write a program using the main method where the user enters a number strictly greater than 0, and the program prints out 3 triangles
(JAVA)
write a program using the main method where the user enters a number strictly greater than 0, and the program prints out 3 triangles of asterisks (*). All triangles must start and end with a single asterisk. All triangles must both increase the number of asterisks per each row until reaching a middle value, and then decrease the number of asterisks until reaching that single point. The middle value of the first triangle is the number that was entered (identifier called number in the provided code), the middle value of the second triangle is two times the number entered, and the last triangles middle value is three times the number entered. Each triangle must be separated using a single end-line. If the user enters an invalid value, then the program must print out Error Invalid Value. Please provide a brief description of how you got the solution as well (explaining methods used, how the program works, etc..)
This is what I have so far, please just copy and add to this solution.
import java.util.Scanner; public class Question01 {
public static void main(String[] args) { int number;//Used for the triangle's middle if(args == null || args.length == 0) { Scanner keyboard = new Scanner(System.in); System.out.println("Enter the value to draw some triangles"); number = keyboard.nextInt(); keyboard.nextLine(); } else { number = Integer.parseInt(args[0]); } //----------------------------------------------------------------------------------- if(number <= 0) { System.out.println("Error Invalid Value"); System.exit(0); }
Solution Tests:
- If the user enters 2, then does the program print out:
*
**
*
*
**
***
****
***
**
*
*
**
***
****
*****
******
*****
****
***
**
*
- If the user enters 3, then does the program print out:
*
**
***
**
*
*
**
***
****
*****
******
*****
****
***
**
*
*
**
***
****
*****
******
*******
********
*********
********
*******
******
*****
****
***
**
*
- If the user enters 0, does the program print out:
Error Invalid Value
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
