Logical Operators Part 4
Coddy Java 여정의 기초 섹션에 포함된 레슨 — 73개 중 26번째.
드 모건의 법칙은 NOT이 포함된 표현식을 단순화하는 데 도움이 되는 규칙입니다. 논리 표현식을 다룰 때, 때때로 이를 단순화하거나 재배열해야 합니다.
두 조건이 and로 연결된 앞에 not이 있을 때, 이를 두 개의 별도 부분으로 나눌 수 있습니다. and는 or로 바뀌고, 각 부분에 각각의 not이 붙습니다:
!(A && B)는 (!A) || (!B)와 같습니다
예를 들어:
// Let's check if a number is NOT (between 1 and 10)
int number = 15;
// These two expressions are equivalent:
boolean result1 = !(number >= 1 && number <= 10);
boolean result2 = !(number >= 1) || !(number <= 10);
System.out.println(result1); // true
System.out.println(result2); // true반대도 성립합니다: !(A || B) 는 (!A) && (!B) 와 같습니다.
예를 들어:
// 사람이 (학생이거나 취업 중이거나)가 아닐 때 확인
boolean isStudent = false;
boolean isEmployed = false;
// 이 두 표현은 동등합니다:
boolean result1 = !(isStudent || isEmployed);
boolean result2 = !(isStudent) && !(isEmployed);
System.out.println(result1); // true
System.out.println(result2); // true챌린지
초급애완동물 가게가 고객에게 애완동물을 판매할 수 있는지 결정하는 시스템을 만드는 데 도움을 주고 있습니다.
다음 변수를 초기화하세요:
hasLicense를 값true로hasSpace를 값false로hasExperience를 값true로
다음 논리 표현식을 작성하여 판단하세요:
canSellRegularPet: 고객은 면허 또는 경험 둘 중 하나를 가져야 하고, 공간이 있어야 합니다canSellExoticPet: 고객은 면허와 경험을 모두 가져야 하고, 공간이 있어야 합니다cannotSellAnyPet: 고객이 면허가 없고 경험이 없거나, 공간이 없습니다result:canSellRegularPet또는canSellExoticPet또는cannotSellAnyPet
팁: 표현식에서 변수 값들을 직접 사용하세요. 예를 들어, hasSpace는 false이므로 "공간이 있어야 함"은 hasSpace를 그대로 (! 없이) 사용한다는 의미입니다 — 변수가 이미 실제 값을 가지고 있기 때문입니다. 조건이 변수가 가진 값의 반대를 요구할 때만 !을 사용하세요. 예: "면허가 없음" → !hasLicense.
치트 시트
드 모건의 법칙은 NOT 연산자를 사용한 논리 표현식을 단순화하는 데 도움이 됩니다:
!(A && B)는 (!A) || (!B)와 동등합니다
!(A || B)는 (!A) && (!B)와 동등합니다
AND 예제:
int number = 15;
boolean result1 = !(number >= 1 && number <= 10);
boolean result2 = !(number >= 1) || !(number <= 10);
// Both expressions are equivalentOR 예제:
boolean isStudent = false;
boolean isEmployed = false;
boolean result1 = !(isStudent || isEmployed);
boolean result2 = !(isStudent) && !(isEmployed);
// Both expressions are equivalent직접 해보기
public class Main {
public static void main(String[] args) {
// 변수 초기화
// 조건 계산
boolean canSellRegularPet =
boolean canSellExoticPet =
boolean cannotSellAnyPet =
boolean result =
// 아래 줄들을 삭제하지 마세요
System.out.println("Can sell regular pet: " + canSellRegularPet);
System.out.println("Can sell exotic pet: " + canSellExoticPet);
System.out.println("Cannot sell any pet: " + cannotSellAnyPet);
System.out.println("Result: " + result);
}
}이 레슨에는 짧은 퀴즈가 포함되어 있습니다. 레슨을 시작해 문제를 풀고 진행 상황을 기록하세요.
기초의 모든 레슨
4Operators Part 1
Arithmetic OperatorsModulo OperatorIncrement/DecrementPost Increment/DecrementArithmetic ShortcutsComparison OperatorsString Comparison5Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Logical Operators Part 43Variables Part 2
ConstantsNaming ConventionsRecap - Initialize VariablesType Casting Part 1Type Casting Part 26Decision Making
If StatementIf - ElseSwitch StatementTernary OperatorRecap - If ElseNested If - Else