Compare commits
No commits in common. 'main' and 'w2' have entirely different histories.
13 changed files with 0 additions and 222 deletions
@ -1,2 +0,0 @@ |
|||||
# java |
|
||||
|
|
||||
Binary file not shown.
@ -1 +0,0 @@ |
|||||
HelloWorld |
|
||||
@ -1,6 +0,0 @@ |
|||||
# TemperatureConverter |
|
||||
温度转换 Java 程序,实现摄氏度 ↔ 华氏度互转 |
|
||||
## 编译 |
|
||||
javac TemperatureConverter.java |
|
||||
## 运行 |
|
||||
java TemperatureConverter |
|
||||
@ -1,49 +0,0 @@ |
|||||
import java.util.Scanner; |
|
||||
public class TemperatureConverter { |
|
||||
/** |
|
||||
* 将摄氏度转换为华氏度。 |
|
||||
* @param c 摄氏温度 |
|
||||
* @return 对应的华氏温度 |
|
||||
*/ |
|
||||
public static double celsiusToFahrenheit(double c) { |
|
||||
return c * 9.0 / 5.0 + 32.0; |
|
||||
} |
|
||||
/** |
|
||||
* 将华氏度转换为摄氏度。 |
|
||||
* @param f 华氏温度 |
|
||||
* @return 对应的摄氏温度 |
|
||||
*/ |
|
||||
public static double fahrenheitToCelsius(double f) { |
|
||||
return (f - 32.0) * 5.0 / 9.0; |
|
||||
} |
|
||||
public static void main(String[] args) { |
|
||||
Scanner scanner = new Scanner(System.in); |
|
||||
// 提示用户输入,格式示例:"36.6 C" 或 "97 F"
|
|
||||
System.out.print("请输入要转换的温度与单位(例如 36.6 C 或 97 F):"); |
|
||||
String input = scanner.nextLine().trim(); |
|
||||
if (input.isEmpty()) { |
|
||||
System.out.println("输入为空,程序退出。"); |
|
||||
return; |
|
||||
} |
|
||||
String[] parts = input.split("\\s+"); // 按任意空白字符分割
|
|
||||
try { |
|
||||
double value = Double.parseDouble(parts[0]); |
|
||||
String unit = (parts.length > 1) ? parts[1].toUpperCase() : "C"; |
|
||||
if (unit.startsWith("C")) { |
|
||||
// 从摄氏度转换为华氏度
|
|
||||
double f = celsiusToFahrenheit(value); |
|
||||
System.out.printf("%.1f °C = %.2f °F%n", value, f); |
|
||||
} else if (unit.startsWith("F")) { |
|
||||
// 从华氏度转换为摄氏度
|
|
||||
double c = fahrenheitToCelsius(value); |
|
||||
System.out.printf("%.1f °F = %.2f °C%n", value, c); |
|
||||
} else { |
|
||||
System.out.println("未知单位,请使用 C 或 F。"); |
|
||||
} |
|
||||
} catch (Exception e) { |
|
||||
System.out.println("输入解析失败,请按示例输入数值与单位,例如 \"36.6 C\""); |
|
||||
} finally { |
|
||||
scanner.close(); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -1,2 +0,0 @@ |
|||||
[LocalizedFileNames] |
|
||||
ÆÁÄ»½ØÍ¼ 2026-03-08 211456.png=@ÆÁÄ»½ØÍ¼ 2026-03-08 211456,0 |
|
||||
|
Before Width: | Height: | Size: 327 KiB |
|
Before Width: | Height: | Size: 161 KiB |
@ -1,81 +0,0 @@ |
|||||
|
|
||||
abstract class Shape { |
|
||||
|
|
||||
public abstract double getArea(); |
|
||||
} |
|
||||
class Circle extends Shape { |
|
||||
private double radius; |
|
||||
public Circle(double radius) { |
|
||||
if (radius <= 0) { |
|
||||
throw new IllegalArgumentException("半径必须为正数"); |
|
||||
} |
|
||||
this.radius = radius; |
|
||||
} |
|
||||
@Override |
|
||||
public double getArea() { |
|
||||
return Math.PI * radius * radius; |
|
||||
} |
|
||||
} |
|
||||
class Rectangle extends Shape { |
|
||||
private double width; |
|
||||
private double height; |
|
||||
public Rectangle(double width, double height) { |
|
||||
if (width <= 0 || height <= 0) { |
|
||||
throw new IllegalArgumentException("宽和高必须为正数"); |
|
||||
} |
|
||||
this.width = width; |
|
||||
this.height = height; |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
public double getArea() { |
|
||||
return width * height; |
|
||||
} |
|
||||
} |
|
||||
class Triangle extends Shape { |
|
||||
private double a; |
|
||||
private double b; |
|
||||
private double c; |
|
||||
public Triangle(double a, double b, double c) { |
|
||||
if (a <= 0 || b <= 0 || c <= 0) { |
|
||||
throw new IllegalArgumentException("边长必须为正数"); |
|
||||
} |
|
||||
if (a + b <= c || a + c <= b || b + c <= a) { |
|
||||
throw new IllegalArgumentException("三边不满足三角形构成条件"); |
|
||||
} |
|
||||
this.a = a; |
|
||||
this.b = b; |
|
||||
this.c = c; |
|
||||
} |
|
||||
@Override |
|
||||
public double getArea() { |
|
||||
double s = (a + b + c) / 2; |
|
||||
return Math.sqrt(s * (s - a) * (s - b) * (s - c)); |
|
||||
} |
|
||||
} |
|
||||
class ShapeUtil { |
|
||||
public static void printArea(Shape shape) { |
|
||||
if (shape == null) { |
|
||||
System.out.println("图形对象不能为空"); |
|
||||
return; |
|
||||
} |
|
||||
System.out.printf("图形面积:%.2f%n", shape.getArea()); |
|
||||
} |
|
||||
} |
|
||||
public class ShapeAreaCalculator { |
|
||||
public static void main(String[] args) { |
|
||||
try { |
|
||||
Shape circle = new Circle(3); |
|
||||
System.out.print("圆形:"); |
|
||||
ShapeUtil.printArea(circle); |
|
||||
Shape rectangle = new Rectangle(4, 5); |
|
||||
System.out.print("矩形:"); |
|
||||
ShapeUtil.printArea(rectangle); |
|
||||
Shape triangle = new Triangle(3, 4, 5); |
|
||||
System.out.print("三角形:"); |
|
||||
ShapeUtil.printArea(triangle); |
|
||||
} catch (IllegalArgumentException e) { |
|
||||
System.out.println("错误:" + e.getMessage()); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 161 KiB |
@ -1,81 +0,0 @@ |
|||||
|
|
||||
abstract class Shape { |
|
||||
|
|
||||
public abstract double getArea(); |
|
||||
} |
|
||||
class Circle extends Shape { |
|
||||
private double radius; |
|
||||
public Circle(double radius) { |
|
||||
if (radius <= 0) { |
|
||||
throw new IllegalArgumentException("半径必须为正数"); |
|
||||
} |
|
||||
this.radius = radius; |
|
||||
} |
|
||||
@Override |
|
||||
public double getArea() { |
|
||||
return Math.PI * radius * radius; |
|
||||
} |
|
||||
} |
|
||||
class Rectangle extends Shape { |
|
||||
private double width; |
|
||||
private double height; |
|
||||
public Rectangle(double width, double height) { |
|
||||
if (width <= 0 || height <= 0) { |
|
||||
throw new IllegalArgumentException("宽和高必须为正数"); |
|
||||
} |
|
||||
this.width = width; |
|
||||
this.height = height; |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
public double getArea() { |
|
||||
return width * height; |
|
||||
} |
|
||||
} |
|
||||
class Triangle extends Shape { |
|
||||
private double a; |
|
||||
private double b; |
|
||||
private double c; |
|
||||
public Triangle(double a, double b, double c) { |
|
||||
if (a <= 0 || b <= 0 || c <= 0) { |
|
||||
throw new IllegalArgumentException("边长必须为正数"); |
|
||||
} |
|
||||
if (a + b <= c || a + c <= b || b + c <= a) { |
|
||||
throw new IllegalArgumentException("三边不满足三角形构成条件"); |
|
||||
} |
|
||||
this.a = a; |
|
||||
this.b = b; |
|
||||
this.c = c; |
|
||||
} |
|
||||
@Override |
|
||||
public double getArea() { |
|
||||
double s = (a + b + c) / 2; |
|
||||
return Math.sqrt(s * (s - a) * (s - b) * (s - c)); |
|
||||
} |
|
||||
} |
|
||||
class ShapeUtil { |
|
||||
public static void printArea(Shape shape) { |
|
||||
if (shape == null) { |
|
||||
System.out.println("图形对象不能为空"); |
|
||||
return; |
|
||||
} |
|
||||
System.out.printf("图形面积:%.2f%n", shape.getArea()); |
|
||||
} |
|
||||
} |
|
||||
public class ShapeAreaCalculator { |
|
||||
public static void main(String[] args) { |
|
||||
try { |
|
||||
Shape circle = new Circle(3); |
|
||||
System.out.print("圆形:"); |
|
||||
ShapeUtil.printArea(circle); |
|
||||
Shape rectangle = new Rectangle(4, 5); |
|
||||
System.out.print("矩形:"); |
|
||||
ShapeUtil.printArea(rectangle); |
|
||||
Shape triangle = new Triangle(3, 4, 5); |
|
||||
System.out.print("三角形:"); |
|
||||
ShapeUtil.printArea(triangle); |
|
||||
} catch (IllegalArgumentException e) { |
|
||||
System.out.println("错误:" + e.getMessage()); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
Binary file not shown.
Loading…
Reference in new issue