For an incorrect option, an appropriate error message should be displayed.
import java.util.Scanner;
public class PatternCreation {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int choice;
do {
System.out.println("Menu:");
System.out.println("1. Print Floyd's Triangle");
System.out.println("2. Display Pattern");
System.out.println("3. Exit");
System.out.print("Enter your choice (1/2/3): ");
choice = scanner.nextInt();
switch (choice) {
case 1:
printFloydsTriangle();
break;
case 2:
displayPattern();
break;
case 3:
System.out.println("Exiting the program.");
break;
default:
System.out.println("Invalid choice. Please select 1, 2, or 3.");
break;
}
} while (choice != 3);
scanner.close();
}
// Method to print Floyd's Triangle
public static void printFloydsTriangle() {
int n = 1;
int rows = 5; // You can change the number of rows as needed
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(n + " ");
n++;
}
System.out.println();
}
}
// Method to display the specified pattern
public static void displayPattern() {
String pattern = "ICSE";
for (int i = 0; i < pattern.length(); i++) {
for (int j = 0; j <= i; j++) {
System.out.print(pattern.charAt(j) + " ");
}
System.out.println();
}
}
}
No comments:
Post a Comment