[자바 연산자] 14. 절차지향 프로그래밍

김건우's avatar
Feb 04, 2025
[자바 연산자] 14. 절차지향 프로그래밍

절차적으로 섭씨-합씨 온도 변환하기

1. 주어진 정보와 섭씨-합씨 공식

package ex02; import java.util.Scanner; public class Ftoc2 { public static void main(String[] args) { // 식1 c = (f - 32) * 5 / 9 // 식2 f = (c * 9 / 5) + 32 System.out.println("===================="); System.out.println("1. 화씨->섭씨"); System.out.println("2. 섭씨->화씨"); System.out.println("===================="); System.out.println(); System.out.print("번호를 선택하시오: ");

2. 합씨 혹은 섭씨 선택하는 코드

// 1. 화씨 혹은 섭씨를 선택하는 로직 Scanner sc = new Scanner(System.in); int n = sc.nextInt();
notion image

3 . 화씨 혹은 섭씨 온도 받는 코드

// 2. 화씨 혹은 섭씨 온도 받기 System.out.print("온도를 입력하시오: "); double f = sc.nextDouble();
notion image

4. 화씨 혹은 섭씨 온도를 받아서 변환하는 코드

// 3. 화씨 혹은 섭씨 온도를 받아서 변환하기 double result = n == 1 ? (5.0 / 9.0 * (f - 32)) : (9.0 / 5.0 * f + 32); System.out.println("온도는 : " + result);
notion image

전체 코드

package ex02; import java.util.Scanner; public class Ftoc2 { public static void main(String[] args) { // 식1 c = (f - 32) * 5 / 9 // 식2 f = (c * 9 / 5) + 32 System.out.println("===================="); System.out.println("1. 화씨->섭씨"); System.out.println("2. 섭씨->화씨"); System.out.println("===================="); System.out.println(); System.out.print("번호를 선택하시오: "); // 1. 화씨 혹은 섭씨를 선택하는 로직 Scanner sc = new Scanner(System.in); int n = sc.nextInt(); // 2. 화씨 혹은 섭씨 온도 받기 System.out.print("온도를 입력하시오: "); double f = sc.nextDouble(); // 3. 화씨 혹은 섭씨 온도를 받아서 변환하기 double result = n == 1 ? (5.0 / 9.0 * (f - 32)) : (9.0 / 5.0 * f + 32); System.out.println("온도는 : " + result); } }
Share article

gunwoo