chapter 4
This commit is contained in:
49
src/chapter4/demo45/Main.java
Normal file
49
src/chapter4/demo45/Main.java
Normal 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();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user