Browse Source

上传文件至 'W1赵若岚202506050221'

Zhaoruolan 1 month ago
parent
commit
bad8808db6
  1. BIN
      W1赵若岚202506050221/IMG_2171.HEIC
  2. BIN
      W1赵若岚202506050221/IMG_2174.HEIC
  3. BIN
      W1赵若岚202506050221/IMG_2175.HEIC
  4. 90
      W1赵若岚202506050221/TemperatureConverter.java
  5. 11
      W1赵若岚202506050221/readme.txt

BIN
W1赵若岚202506050221/IMG_2171.HEIC

Binary file not shown.

BIN
W1赵若岚202506050221/IMG_2174.HEIC

Binary file not shown.

BIN
W1赵若岚202506050221/IMG_2175.HEIC

Binary file not shown.

90
W1赵若岚202506050221/TemperatureConverter.java

@ -0,0 +1,90 @@
import java.util.Scanner;
/**
* Temperature Converter (Java Version)
* Supports conversion between Celsius (C) and Fahrenheit (F).
*
* Features:
* 1. Interactive input, for example: 36.6 C or 97 F
* 2. Command-line argument mode, for example:
* java TemperatureConverter 36.6 C
*/
public class TemperatureConverter {
/**
* Converts Celsius to Fahrenheit.
*
* @param c temperature in Celsius
* @return equivalent temperature in Fahrenheit
*/
public static double celsiusToFahrenheit(double c) {
return c * 9.0 / 5.0 + 32.0;
}
/**
* Converts Fahrenheit to Celsius.
*
* @param f temperature in Fahrenheit
* @return equivalent temperature in Celsius
*/
public static double fahrenheitToCelsius(double f) {
return (f - 32.0) * 5.0 / 9.0;
}
/**
* Main method of the program.
*
* Input examples:
* 1. Interactive mode: 36.6 C
* 2. Command-line mode: java TemperatureConverter 36.6 C
*
* @param args command-line arguments
*/
public static void main(String[] args) {
String input;
// If command-line arguments are provided, use them first
if (args.length > 0) {
StringBuilder sb = new StringBuilder();
for (String arg : args) {
sb.append(arg).append(" ");
}
input = sb.toString().trim();
} else {
// Prompt the user for input, for example: "36.6 C" or "97 F"
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the temperature and unit to convert (for example 36.6 C or 97 F): ");
input = scanner.nextLine().trim();
}
if (input.isEmpty()) {
System.out.println("Input is empty. Program terminated.");
return;
}
String[] parts = input.split("\\s+");
double value;
String unit;
try {
// Allow the user to enter a numeric value and a unit
value = Double.parseDouble(parts[0]);
unit = (parts.length > 1) ? parts[1].toUpperCase() : "C";
} catch (Exception e) {
System.out.println("Failed to parse input. Please enter a value and unit like: 36.6 C");
return;
}
if (unit.startsWith("C")) {
// Convert from Celsius to Fahrenheit
double f = celsiusToFahrenheit(value);
System.out.printf("%.1f °C = %.2f °F%n", value, f);
} else if (unit.startsWith("F")) {
// Convert from Fahrenheit to Celsius
double c = fahrenheitToCelsius(value);
System.out.printf("%.1f °F = %.2f °C%n", value, c);
} else {
System.out.println("Unknown unit. Please use C or F.");
}
}
}

11
W1赵若岚202506050221/readme.txt

@ -0,0 +1,11 @@
READ ME.md
# TemperatureConverter
This is a Java version of the Python temperature conversion program.
It supports conversion between Celsius (C) and Fahrenheit (F).
## Compile
```bash
javac TemperatureConverter.java
Loading…
Cancel
Save