Write a program using a switch statement that displays the name of a polygon wit
ID: 3552171 • Letter: W
Question
Write a program using a switch statement that displays the name of a polygon with sides between 3 and 12 depending on the number entered by the user. For example, if the user enters 3, the program should display the string "Triangle". if the user enters 8, the program should display the string "Octogon". If the user enters an invalid number (assume 1 side and 2 sides cannot be polygons), and ( 13 and above are out of the range of the program)...the program should display an appropriate error message.
Thank you
Explanation / Answer
import java.util.*;
public class aaaaaaaaaaaaa {
public static void main(String[] args) {
int n;
Scanner sc=new Scanner(System.in);
System.out.println("enter number of sides in polygon :");
String str=sc.nextLine();
n=Integer.parseInt(str);
switch(n)
{
case 1:
System.out.println("Not a valid polygon .");
break;
case 2:
System.out.println("Not a valid polygon .");
break;
case 3:
System.out.println("Polygon is Triangle .");
break;
case 4:
System.out.println("Polygon is Quadrilateral .");
break;
case 5:
System.out.println("Polygon is Pentagonal .");
break;
case 6:
System.out.println("Polygon is Hexagonal .");
break;
case 7:
System.out.println("Polygon is heptagonal .");
break;
case 8:
System.out.println("Polygon is octagonal .");
break;
case 9:
System.out.println("Polygon is Nonagonal .");
break;
case 10:
System.out.println("Polygon is decagonal.");
break;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.