비교 연산자
Coddy Dart 여정의 기초 섹션에 포함된 레슨 — 94개 중 22번째.
비교 연산자는 Dart에서 불리언 값을 반환하며, 의사 결정에 필수적입니다.
void main() {
int a = 10;
int b = 10;
bool areEqual = a == b;
print('Is $a equal to $b? $areEqual');
}Dart는 다음과 같은 비교 연산자를 제공합니다: 같음(==), 다름(!=), 초과(>), 미만(<), 이상(>=), 그리고 이하(<=).
챌린지
초급비교 연산자를 사용하여 두 게임 점수를 비교하는 프로그램을 만드세요:
- 값이 85인
playerScore와 값이 90인highScore두 개의 정수 변수를 선언하세요 - 비교 연산자를 사용하여 다음을 확인하세요:
playerScore가highScore와 같은지playerScore가highScore와 같지 않은지playerScore가 80보다 큰지playerScore가highScore보다 작은지playerScore가 85 이상인지playerScore가 100 이하인지- 각 비교 결과를 설명적인 이름의 불리언 변수에 저장하세요
- 예상 출력에 정확히 표시된 대로 모든 결과를 레이블과 함께 출력하세요
출력은 이 정확한 형식을 따라야 합니다:
Player score: 85 High score: 90 Scores are equal: false Scores are different: true Player score > 80: true Player score < High score: true Player score >= 85: true Player score <= 100: true
치트 시트
Dart의 비교 연산자는 의사 결정을 위해 불리언 값을 반환합니다:
==- 동등성!=- 불평등>- 크다<- 작다>=- 크거나 같다<=- 작거나 같다
void main() {
int a = 10;
int b = 10;
bool areEqual = a == b;
print('Is $a equal to $b? $areEqual');
}직접 해보기
void main() {
// 여기서 점수 변수를 선언하세요
int playerScore = ?;
int highScore = ?;
// 비교를 수행하고 결과를 boolean 변수에 저장하세요
bool scoresEqual = playerScore ? highScore;
bool scoresDifferent = playerScore ? highScore;
bool scoreAbove80 = playerScore ? 80;
bool scoreLessThanHigh = playerScore ? highScore;
bool scoreAtLeast85 = playerScore ? 85;
bool scoreAtMost100 = playerScore ? 100;
// 점수와 비교 결과를 출력하세요
print("Player score: $playerScore");
print("High score: $highScore");
print("Scores are equal: $scoresEqual");
print("Scores are different: $scoresDifferent");
print("Player score > 80: $scoreAbove80");
print("Player score < High score: $scoreLessThanHigh");
print("Player score >= 85: $scoreAtLeast85");
print("Player score <= 100: $scoreAtMost100");
}이 레슨에는 짧은 퀴즈가 포함되어 있습니다. 레슨을 시작해 문제를 풀고 진행 상황을 기록하세요.