chapter 4

This commit is contained in:
2024-10-23 12:51:53 +08:00
parent 8737a64402
commit 761fcb4905
10 changed files with 280 additions and 0 deletions

View File

@@ -0,0 +1,49 @@
package chapter4.demo45;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// 处理用户输入
Scanner sc = new Scanner(System.in);
while(true) {
// 打印菜单
ShapeCalculate.printMenu();
int select = sc.nextInt();
// 判断用户选项
switch (select) {
case 1: // 圆形
System.out.print("请输入圆形的半径:");
double radius = sc.nextDouble();
Shape circle = new Circle(radius);
ShapeCalculate.printArea(circle);
ShapeCalculate.printPerimeter(circle);
break;
case 2: // 长方形
System.out.print("请输入长方形的长和宽:");
double length = sc.nextDouble();
double width = sc.nextDouble();
Shape rectangle = new Rectangle(length, width);
ShapeCalculate.printArea(rectangle);
ShapeCalculate.printPerimeter(rectangle);
break;
default:
System.out.println("输入错误,请重新选择");
ShapeCalculate.printMenu();
break;
}
// 询问用户是否继续
System.out.println("继续计算Y / N");
String answer = sc.next();
if ("N".equalsIgnoreCase(answer)) { // 无论大小写,都识别
break;
}
}
System.out.println("感谢您的使用,再见!");
sc.close();
}
}