개발자노트

If 예제 (시간계산)

hansummer 2022. 6. 9. 18:26
package parctice;

import java.util.Scanner;

public class Line {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		System.out.print("시간: ");

		int h = sc.nextInt();

		System.out.print("분: ");

		int m = sc.nextInt();

		if ((h < 1 || 12 < h) || (m < 0 || 59 < m)) {

			// 유효성 체크!

			System.out.println("시간 범위: 1~12");

			System.out.println("분 범위: 0~59");

		}

		else {

			System.out.println(h + "시 " + m + "분의 1시간 20분전시간은");

			h--;

			m -= 20;

			if (m < 0) {

				m += 60;

				h--;

			}

			if (h < 1) { // ☆ h라는 값이 다 사용되고 나서 적용되는 위치에 쓰기(로직)

				h += 12;

			}

			System.out.println(h + "시 " + m + "분입니다.");

		}

		// 검사 == 테스트

		// 1) 경계값 검사

	}

}