Algorithm

Codingbat-Java] Warmup1 - parrotTrouble

hololo 2021. 9. 24. 18:24

문제

parameter

  • talking : parrot이 말하는지
  • hour : 현재 시간

description

We have a loud talking parrot. The "hour" parameter is the current hour time in the range 0..23. We are in trouble if the parrot is talking and the hour is before 7 or after 20. Return true if we are in trouble.

parrotTrouble(true, 6) → true
parrotTrouble(true, 7) → false
parrotTrouble(false, 6) → false

parrot이 말을 하고, 시간이 7시 전 or 20시 후이면 문제가 발생 -> true 반환

풀이 코드

public boolean parrotTrouble(boolean talking, int hour) {
  boolean dangerHour = hour<7 || hour>20 ? true:false;
  return talking && dangerHour;
}

풀이

식 : parrot이 말을 한다 && (7시 전 || 20시 후)

1) 시간에 대한 boolean 변수를 두고 true와 false를 저장
2) talking 여부와 && 연산

 

Show Solution