2024-10-29 09:17:12 +08:00

52 lines
1.8 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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();
Circle circle = new Circle(radius);
// 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);
Rectangle 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();
}
}