All menu options work besides menu option 1 when i select 1 it just repeats the menu instead of asking me to type the name and repeat it 20 time, if anyone can help me debug I would appreciate it.
Get the user’s first name and echo it back out 20 times.
Get the Store user’s age and double it and display the age and the doubled age.
Using the age from #2 output one of the following statements.Since you are 99 years old, you are a teenagerb. Since you are 99 years old, you are NOT a teenager
Get a single integer between 3 and 50 from the user. Create a triangle of X’s with the integer inputted rows. The triangle needs to be displayed on the screen and in a text document named triangle.txt.
The code :
//Menu.java
import java.util.Scanner;
public class Menu {
public static void main(String[] args) {
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
int choice;
do {
System.out.println("\n::MENU::");
System.out.println("1.Echo Name");
System.out.println("2.Double Your Age");
System.out.println("3.Check Teenager or not");
System.out.println("4.Display Triangle");
System.out.println("5.Exit");
System.out.print("Enter Choice:");
choice = sc.nextInt();
switch (choice) {
case 1: {
System.out.print("Enter your name");
String name = sc.nextLine();
for (int i = 1; i <= 20; i++) {
System.out.println(name);
}
break;
}
case 2: {
int age;
System.out.print("Enter age :");
age = sc.nextInt();
System.out.println("Your age :" + age);
System.out.println("Doubled age :" + (2 * age));
break;
}
case 3: {
int age;
System.out.print("Enter age :");
age = sc.nextInt();
if (age >= 13 && age <= 19) {
System.out.println("Since you are " + age + " years old.Your are a Teenager");
} else {
System.out.println("Since you are " + age + " years old.Your are not a Teenager");
}
break;
}
case 4: {
int rows;
do {
System.out.print("Enter a number (between 3 and 50):");
rows = sc.nextInt();
if (rows < 3 || rows > 50) {
System.out.println("** Invalid.Must be between 3 and 50 **");
}
} while (rows < 3 || rows > 50);
printTriangle(rows);
break;
}
case 5: {
break;
}
default: {
System.out.println("** Invalid Choice **");
break;
}
}
} while (choice != 5);
}
private static void printTriangle(int size) {
char ch = '*';
for (int a = 0; a < 2 * size - 1; a++) {
if (a % 2 == 0) {
for (int b = 0; b < (2 * size - 1) - a; b++) {
System.out.print(" ");
}
for (int c = 0; c <= a; c++) {
System.out.print(ch + " ");
}
System.out.println();
}
}
}
}