20230129 간단한 수학시험을 보는 프로그램을 만들며 배운 것들

Daisy🌷·2023년 1월 29일
0

간단한 수학시험을 보는 프로그램을 만들기

[요구사항]

  • 숫자 2개를 입력 받음.
  • 숫자 2개의 합을 입력하라고 요구함.
  • 숫자 2개의 곱을 입력하라고 요구함.
  • 답을 입력하면 Genius!, Muggle!, Stupid!를 출력
    (둘 다 맞추면 천재, 하나만 맞추면 머글, 둘 다 틀리면 바보)

중복을 발견 > 패턴 발견 > 중복을 제거

1. 중복을 발견

answer1 == x + y
answer2 == x * y 

2. 중복을 제거

1단계) result1이라는 answer1과 x + y 값을 비교하는 boolean 변수를 생성

boolean result1 = answer1 == x + y

2단계) result2라는 answer2와 x * y 값을 비교하는 boolean 변수를 생성

boolean result2 = answer2 == x * y

3단계) result1과 result2 모두 true일 때, "Genius"를 출력하므로
조건식 : result1 && result2

if (result1 && result2) {
            System.out.println("Genius");
        }

4단계) result1이 true이고 result2가 false일 때
또는 result1이 false이고 result2가 true일 때 "Muggle!"을 출력하므로
조건식 : result1 && !result2 || !result1 && result2

  • 연산자 우선순위를 따져보았을 때 && 연산자의 순위가 || 연산자의 순위보다 높으므로 요구사항에 맞게 조건식이 잘 만들어진 것임.
  • ! : not(부정) / true는 false로 false는 true로 나타냄.
if (result1 && !result2 || !result1 && result2) {
            System.out.println("Muggle!");
        }

5단계) result1과 result2가 모두 false일 때, "Stupid"를 출력하므로
조건식 : !result1 && !result2

if (!result1 && !result2) {
            System.out.println("Stupid!");
        }
profile
티스토리로 블로그를 이전했습니다. 😂 구경 오세요! 👉🏻 https://u-ryu-logs.tistory.com

0개의 댓글