논리 AND
Coddy Dart 여정의 기초 섹션에 포함된 레슨 — 94개 중 23번째.
논리 AND 연산자(&&)는 두 표현식이 모두 true일 때만 true를 반환하며, 그렇지 않으면 false를 반환합니다.
void main() {
bool isAdult = true;
bool hasLicense = true;
bool canDrive = isAdult && hasLicense;
print('Can this person drive? $canDrive');
}void main() {
int age = 25;
double savings = 5000.0;
bool isEligibleForLoan = (age >= 18) && (savings >= 1000.0);
print('Is eligible for loan? $isEligibleForLoan');
}챌린지
쉬움논리적 AND (&&) 연산자를 사용하여 게임 자격 확인기를 만드세요:
- 15의 값으로 정수 변수
age를 선언하세요 - true 값으로 불리언 변수
hasParentalConsent를 선언하세요 - false 값으로 불리언 변수
hasCompletedTutorial를 선언하세요 - 다음 조건에서만 true인 불리언 변수
canPlayGame를 선언하세요: - 플레이어가 18세 이상이거나,
- 플레이어가 최소 13세 이상이고 부모 동의가 있는 경우
- 다음 조건에서만 true인 불리언 변수
canAccessBonus를 선언하세요: - 플레이어가
canPlayGame이고 튜토리얼을 완료한 경우 - 정확히 다음 형식으로 결과를 출력하세요:
Player age: 15
Has parental consent: true
Completed tutorial: false
Can play game: true
Can access bonus content: false출력은 이 정확한 형식을 따라야 합니다.
치트 시트
논리 AND 연산자 (&&)는 두 표현식이 모두 true일 때만 true를 반환하며, 그렇지 않으면 false를 반환합니다.
bool canDrive = isAdult && hasLicense;여러 조건을 결합할 수 있습니다:
bool isEligible = (age >= 18) && (savings >= 1000.0);직접 해보기
void main() {
// 여기에 변수를 선언하세요
int age = ?;
bool hasParentalConsent = ?;
bool hasCompletedTutorial = ?;
// 플레이어가 게임을 플레이할 수 있는지 확인
bool canPlayGame = age >= ? || (age >= ? && hasParentalConsent);
// 플레이어가 보너스 콘텐츠에 접근할 수 있는지 확인
bool canAccessBonus = canPlayGame ? hasCompletedTutorial;
// 결과를 출력
print("Player age: $age");
print("Has parental consent: $hasParentalConsent");
print("Completed tutorial: $hasCompletedTutorial");
print("Can play game: $canPlayGame");
print("Can access bonus content: $canAccessBonus");
}이 레슨에는 짧은 퀴즈가 포함되어 있습니다. 레슨을 시작해 문제를 풀고 진행 상황을 기록하세요.