我创建了一个带有 switch 语句的类,它根据用户选择的边数打印出多边形的名称。我无法弄清楚的问题是当边小于 3 且大于 12 时如何执行此操作。我宁愿使用 if 语句,但我不能用于这部分。因为我可以使用一个案例:对于每个超过 12 的数字,例如案例 13:、案例 14:等。我该怎么做?
import java.util.Scanner;
public class Lab13 {
public static void main(String[] args) {
int sides = 0;
Scanner scan = new Scanner(System. in );
System.out.println("Please enter the number of sides of your Polygon");
sides = scan.nextInt();
String polygonname = "";
switch (sides) {
case 3:
polygonname = polygonname + "triangle";
break;
case 4:
polygonname = polygonname + "square";
break;
case 5:
polygonname = polygonname + "pentagon";
break;
case 6:
polygonname = polygonname + "hexagon";
break;
case 7:
polygonname = polygonname + "heptagon";
break;
case 8:
polygonname = polygonname + "octagon";
break;
case 9:
polygonname = polygonname + "nonagon";
break;
case 10:
polygonname = polygonname + "decagon";
break;
case 12:
polygonname = polygonname + "dodecagon";
break;
}
System.out.print("A polygon with " + sides + " sides is called a " + polygonname + ".");
}
}