You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
3.5 KiB

{\rtf1\ansi\ansicpg936\cocoartf2822
\cocoatextscaling0\cocoaplatform0{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
{\colortbl;\red255\green255\blue255;}
{\*\expandedcolortbl;;}
\paperw11900\paperh16840\margl1440\margr1440\vieww11520\viewh8400\viewkind0
\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural\partightenfactor0
\f0\fs24 \cf0 import java.util.Scanner;\
\
/**\
* TemperatureConverter\
* \uc0\u25903 \u25345 \u25668 \u27663 \u24230 (C)\u19982 \u21326 \u27663 \u24230 (F)\u20043 \u38388 \u20114 \u36716 \
*/\
public class TemperatureConverter \{\
\
/**\
* \uc0\u23558 \u25668 \u27663 \u24230 \u36716 \u25442 \u20026 \u21326 \u27663 \u24230 \
* @param c \uc0\u25668 \u27663 \u28201 \u24230 \
* @return \uc0\u23545 \u24212 \u30340 \u21326 \u27663 \u28201 \u24230 \
*/\
public static double celsiusToFahrenheit(double c) \{\
return c * 9.0 / 5.0 + 32.0;\
\}\
\
/**\
* \uc0\u23558 \u21326 \u27663 \u24230 \u36716 \u25442 \u20026 \u25668 \u27663 \u24230 \
* @param f \uc0\u21326 \u27663 \u28201 \u24230 \
* @return \uc0\u23545 \u24212 \u30340 \u25668 \u27663 \u28201 \u24230 \
*/\
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);\
\
// \uc0\u25552 \u31034 \u29992 \u25143 \u36755 \u20837 \u65292 \u26684 \u24335 \u31034 \u20363 \u65306 "36.6 C" \u25110 "97 F"\
System.out.print("\uc0\u35831 \u36755 \u20837 \u35201 \u36716 \u25442 \u30340 \u28201 \u24230 \u19982 \u21333 \u20301 \u65288 \u20363 \u22914 36.6 C \u25110 97 F\u65289 \u65306 ");\
String input = scanner.nextLine().trim();\
\
if (input.isEmpty()) \{\
System.out.println("\uc0\u36755 \u20837 \u20026 \u31354 \u65292 \u31243 \u24207 \u36864 \u20986 \u12290 ");\
scanner.close();\
return;\
\}\
\
String[] parts = input.split("\\\\s+");\
\
try \{\
// \uc0\u35299 \u26512 \u25968 \u20540 \u21644 \u21333 \u20301 \
double value = Double.parseDouble(parts[0]);\
String unit = parts.length > 1 ? parts[1].toUpperCase() : "C";\
\
if (unit.startsWith("C")) \{\
// \uc0\u20174 \u25668 \u27663 \u24230 \u36716 \u25442 \u20026 \u21326 \u27663 \u24230 \
double f = celsiusToFahrenheit(value);\
System.out.printf("%.2f \'b0C = %.2f \'b0F%n", value, f);\
\} else if (unit.startsWith("F")) \{\
// \uc0\u20174 \u21326 \u27663 \u24230 \u36716 \u25442 \u20026 \u25668 \u27663 \u24230 \
double c = fahrenheitToCelsius(value);\
System.out.printf("%.2f \'b0F = %.2f \'b0C%n", value, c);\
\} else \{\
System.out.println("\uc0\u26410 \u30693 \u21333 \u20301 \u65292 \u35831 \u20351 \u29992 C \u25110 F\u12290 ");\
\}\
\
\} catch (NumberFormatException e) \{\
System.out.println("\uc0\u36755 \u20837 \u35299 \u26512 \u22833 \u36133 \u65292 \u35831 \u25353 \u31034 \u20363 \u36755 \u20837 \u25968 \u20540 \u19982 \u21333 \u20301 \u65292 \u20363 \u22914 \u65306 36.6 C");\
\} catch (Exception e) \{\
System.out.println("\uc0\u21457 \u29983 \u38169 \u35823 \u65306 " + e.getMessage());\
\} finally \{\
scanner.close();\
\}\
\}\
\}}