From e7a8630bde9bf74c140b8c5f1fbda953399fb68e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=8F=AF=E6=96=B0?= <2168202688@qq.com> Date: Fri, 29 Mar 2024 17:52:07 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A0=B9=E6=8D=AE=E9=98=BF=E9=87=8C=E5=B7=B4?= =?UTF-8?q?=E5=B7=B4JAVA=E4=BB=A3=E7=A0=81=E8=A7=84=E8=8C=83=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ShapeDrawer.java | 126 ++++++++++++++++++++++++++++--------------- 1 file changed, 84 insertions(+), 42 deletions(-) diff --git a/src/ShapeDrawer.java b/src/ShapeDrawer.java index 3858c12..b1c4294 100644 --- a/src/ShapeDrawer.java +++ b/src/ShapeDrawer.java @@ -1,88 +1,130 @@ import java.util.Scanner; - -public class -ShapeDrawer - - - -{ - +/** + * @author 美美新新 + */ +public class ShapeDrawer { + + private static final String SQUARE = "正⽅形"; + private static final String CIRCLE = "圆形"; + private static final String TRIANGLE = "三⻆形"; + private static final String TRAPEZOID = "梯形"; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); - - // 用户选择形状 - System.out.println("请输入形状名称(正方形、圆形、三角形 或 梯形):"); - String shape = scanner.nextLine - ().trim().toLowerCase(); - - // 根据用户选择调用相应的方法 - if ("正方形".equals(shape)) { + // ⽤户选择形状 + System.out.println("请输⼊形状名称(正⽅形、圆形、三⻆形 或 梯 形):"); + String shape = scanner.nextLine().trim().toLowerCase(); + // 根据⽤户选择调⽤相应的⽅法 + if (SQUARE.equals(shape)) { calculateSquareArea(scanner); - } else if ("圆形".equals(shape)) { + } else if (CIRCLE.equals(shape)) { calculateCircleArea(scanner); - } else if ("三角形".equals(shape)) { + } else if (TRIANGLE.equals(shape)) { calculateTriangleArea(scanner); - } else if ("梯形".equals(shape)) { + } else if (TRAPEZOID.equals(shape)) { calculateTrapezoidArea(scanner); } else { - System.out.println("不支持的形状类型。"); + System.out.println("不⽀持的形状类型。"); } - scanner.close(); + } - // 计算正方形面积的方法 + /** + * 计算正⽅形⾯积的⽅法 + * + * @param scanner ⽤于接收⽤户输⼊的Scanner对象 + */ public static void calculateSquareArea(Scanner scanner) { - System.out.println("请输入正方形的边长:"); + System.out.println("请输⼊正⽅形的边⻓:"); double side = scanner.nextDouble(); - System.out.println("正方形的面积是:" + squareArea(side)); + System.out.println("正⽅形的⾯积是:" + squareArea(side)); } - // 计算圆形面积的方法 + /** + * 计算圆形⾯积的⽅法 + * + * @param scanner ⽤于接收⽤户输⼊的Scanner对象 + */ public static void calculateCircleArea(Scanner scanner) { - System.out.println("请输入圆形的半径:"); + System.out.println("请输⼊圆形的半径:"); double radius = scanner.nextDouble(); - System.out.println("圆形的面积是:" + circleArea(radius)); + System.out.println("圆形的⾯积是:" + circleArea(radius)); } - // 计算三角形面积的方法 + /** + * 计算三⻆形⾯积的⽅法 + * + * @param scanner ⽤于接收⽤户输⼊的Scanner对象 + */ public static void calculateTriangleArea(Scanner scanner) { - System.out.println("请输入三角形的三条边长:"); + System.out.println("请输⼊三⻆形的三条边⻓:"); double a = scanner.nextDouble(); double b = scanner.nextDouble(); double c = scanner.nextDouble(); - System.out.println("三角形的面积是:" + triangleArea(a, b, c)); + System.out.println("三⻆形的⾯积是:" + triangleArea(a, b, c)); } - // 计算梯形面积的方法 + /** + * 计算梯形⾯积的⽅法 + * + * @param scanner ⽤于接收⽤户输⼊的Scanner对象 + */ public static void calculateTrapezoidArea(Scanner scanner) { - System.out.println("请输入梯形的上底长度:"); + System.out.println("请输⼊梯形的上底⻓度:"); double base1 = scanner.nextDouble(); - System.out.println("请输入梯形的下底长度:"); + System.out.println("请输⼊梯形的下底⻓度:"); double base2 = scanner.nextDouble(); - System.out.println("请输入梯形的高:"); + System.out.println("请输⼊梯形的⾼:"); double height = scanner.nextDouble(); - System.out.println("梯形的面积是:" + trapezoidArea(base1, base2, height)); + System.out.println("梯形的⾯积是:" + trapezoidArea(base1, base2, + height)); } - // 静态方法:计算正方形面积 + /** + * 计算正⽅形⾯积的静态⽅法 + * + * @param side 正⽅形的边⻓ + * @return 返回正⽅形的⾯积 + */ public static double squareArea(double side) { return side * side; } - // 静态方法:计算圆形面积 + /** + * 计算圆形⾯积的静态⽅法 + * + * @param radius 圆形的半径 + * @return 返回圆形的⾯积 + */ public static double circleArea(double radius) { return Math.PI * radius * radius; } - // 静态方法:计算三角形面积(使用海伦公式) + /** + * 计算三⻆形⾯积的静态⽅法(使⽤海伦公式) + * + * @param a 三⻆形的第⼀条边⻓ + * @param b 三⻆形的第⼆条边⻓ + * @param c 三⻆形的第三条边⻓ + * @return 返回三⻆形的⾯积 + */ public static double triangleArea(double a, double b, double c) { double s = (a + b + c) / 2; return Math.sqrt(s * (s - a) * (s - b) * (s - c)); } - // 静态方法:计算梯形面积 - public static double trapezoidArea(double base1, double base2, double height) { - return base1 + base2 * height / 2; + /** + * 计算梯形⾯积的静态⽅法 + * + * @param base1 梯形的上底⻓度 + * @param base2 梯形的下底⻓度 + * @param height 梯形的⾼ + * @return 返回梯形的⾯积 + */ + public static double trapezoidArea(double base1, double base2, double + height) { + + return (base1 + base2) * height / 2; } -} \ No newline at end of file +} + -- Gitee