[Java] 6. 연산자_6.섭씨온도문제

김미숙's avatar
Feb 05, 2025
[Java] 6. 연산자_6.섭씨온도문제

1. 화씨 → 섭씨

package ex02; import java.util.Scanner; public class FtoC1 { public static void main(String[] args) { // 식1 C = 5/9(F - 32) // 식2 F = (1.8*C) + 32 double f; double c; // 1. 화씨(F) 온도를 받아서 System.out.println("==============="); System.out.println("1. 화씨 - > 섭씨"); System.out.println("==============="); System.out.println(); System.out.println("화씨 온도를 입력하시오: "); Scanner sc = new Scanner(System.in); f = sc.nextDouble(); // 2. 섭씨(C) 온도로 환산 c = 5.0 / 9.0 * (f - 32.0); // 3. 모니터 출력 System.out.println(c);
C:\workspace\tools\jdk-21\bin\java.exe "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2024.3.2.1\lib\idea_rt.jar=52391:C:\Program Files\JetBrains\IntelliJ IDEA 2024.3.2.1\bin" -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8 -classpath C:\workspace\java_lec\study\out\production\study ex02.FtoC1 =============== 1. 화씨 - > 섭씨 =============== 화씨 온도를 입력하시오: 100 37.77777777777778 Process finished with exit code 0
 

2. 섭씨 → 화씨

package ex02; import org.w3c.dom.ls.LSOutput; import java.util.Scanner; public class FtoC2 { public static void main(String[] args) { // 식1 C = 5/9(F - 32) // 식2 F = (1.8*C) + 32 double c; double f; // 1. 섭씨(C) 온도를 받아서 System.out.println("==============="); System.out.println("1. 섭씨 - > 화씨"); System.out.println("==============="); System.out.println(); System.out.println("섭씨 온도를 입력하시오: "); Scanner input = new Scanner(System.in); c = input.nextDouble(); // 2. 화씨(F) 온도로 환산 f = (1.8 * c) + 32; // 3. 모니터 출력 System.out.println("섭씨 온도는 " + f);
C:\workspace\tools\jdk-21\bin\java.exe "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2024.3.2.1\lib\idea_rt.jar=52420:C:\Program Files\JetBrains\IntelliJ IDEA 2024.3.2.1\bin" -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8 -classpath C:\workspace\java_lec\study\out\production\study ex02.FtoC2 =============== 1. 섭씨 - > 화씨 =============== 섭씨 온도를 입력하시오: 37.77777777777778 섭씨 온도는 100.0 Process finished with exit code 0
 

3. 섭씨, 화씨 둘 중 선택 (삼항연산자 사용)

package ex02; import java.util.Scanner; public class FtoC3 { public static void main(String[] args) { // 1. 화씨 혹은 섭씨 선택 Scanner sc = new Scanner(System.in); int selectednumber = sc.nextInt(); // 2. 화씨 혹은 섭씨 온도 받기 System.out.println("온도를 입력하시오: "); double temp = sc.nextDouble(); // 3. 화씨 혹은 섭씨 온도 받아서 변환하기 double result = selectednumber == 1 ? (5.0 / 9.0 * (temp - 32)) : (1.8 * temp + 32); System.out.println("온도는: " + result);
C:\workspace\tools\jdk-21\bin\java.exe "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2024.3.2.1\lib\idea_rt.jar=52313:C:\Program Files\JetBrains\IntelliJ IDEA 2024.3.2.1\bin" -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8 -classpath C:\workspace\java_lec\study\out\production\study ex02.FtoC3 100 온도를 입력하시오: 37.77777777777778 온도는: 100.0 Process finished with exit code 0
Share article

parangdajavous