Untitled
unknown
plain_text
5 months ago
5.5 kB
4
Indexable
import java.util.Scanner; // Add Scanner class to allow user input from console public class Main { public static void main(String[] args) { // Create a Scanner object to read user input Scanner scanner = new Scanner(System.in); boolean continueProgram = true; // Control loop for continuing the program // Loop until the user decides to exit while (continueProgram) { // Ask the User for Input - What Shape? System.out.println("Enter the shape (triangle, diamond, rectangle, heart):"); String shape = scanner.nextLine().toLowerCase(); // Ask the User for the size of the shape System.out.println("Enter the size:"); int size = scanner.nextInt(); // Ask the User for what type of Character to use for the Shape System.out.println("Enter the character to use for the shape:"); char ch = scanner.next().charAt(0); scanner.nextLine(); // Consume newline after character input // Print the shape based on the user input switch (shape) { case "triangle": printTriangle(size, ch); break; case "diamond": printDiamond(size, ch); break; case "rectangle": printRectangle(size, ch); break; case "heart": printHeart(size, ch); break; default: System.out.println("Invalid shape entered! Please try again."); continue; // Restart the loop on invalid input } // Ask the user if they want to print another shape System.out.println("Do you want to print another shape? (yes/no):"); String response = scanner.nextLine().toLowerCase(); if (!response.equals("yes")) { continueProgram = false; // Exit the loop if the user enters anything other than "yes" } } System.out.println("Goodbye!"); // Farewell message before exiting scanner.close(); // Close the scanner object } // Method to print a right-aligned triangle public static void printTriangle(int size, char ch) { for (int i = 1; i <= size; i++) { for (int j = 1; j <= size - i; j++) { System.out.print(" "); } for (int k = 1; k <= 2 * i - 1; k++) { System.out.print(ch); } System.out.println(); } } // Method to print a diamond public static void printDiamond(int size, char ch) { int n = size / 2 + 1; // Upper part of the diamond for (int i = 1; i <= n; i++) { for (int j = 1; j <= n - i; j++) { System.out.print(" "); } for (int k = 1; k <= 2 * i - 1; k++) { System.out.print(ch); } System.out.println(); } // Lower part of the diamond for (int i = n - 1; i >= 1; i--) { for (int j = 1; j <= n - i; j++) { System.out.print(" "); } for (int k = 1; k <= 2 * i - 1; k++) { System.out.print(ch); } System.out.println(); } } // Method to print a rectangle public static void printRectangle(int size, char ch) { for (int i = 1; i <= size; i++) { for (int j = 1; j <= size; j++) { System.out.print(ch); } System.out.println(); } } // Method to print a heart pattern with the given size and character public static void printHeart(int size, char ch) { // Ensure the size is valid for a heart shape if (size < 3) { System.out.println("Size too small for a heart shape. Please enter a size of 3 or more."); return; } // Upper part of the heart (two lobes) for (int m = 0; m < size; m++) { for (int n = 0; n <= 4 * size; n++) { // Calculate the positions for the two circular lobes double pos1 = Math.sqrt(Math.pow(m - size, 2) + Math.pow(n - size, 2)); double pos2 = Math.sqrt(Math.pow(m - size, 2) + Math.pow(n - 3 * size, 2)); // Print the character if within the lobe, otherwise print space if (pos1 < size + 0.5 || pos2 < size + 0.5) { System.out.print(ch); } else { System.out.print(' '); } } System.out.println(); // Move to the next line after printing one row } // Lower part of the heart (tapering bottom) for (int m = 1; m <= 2 * size; m++) { // Print leading spaces to align the bottom part for (int n = 0; n < m; n++) { System.out.print(' '); } // Print the tapering bottom part for (int n = 0; n < 4 * size + 1 - 2 * m; n++) { System.out.print(ch); } System.out.println(); // Move to the next line after printing one row } } }
Editor is loading...
Leave a Comment