論理 AND
CoddyのDartジャーニー「基礎」セクションの一部。レッスン 23/94。
論理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を宣言します: playerが 18 歳以上、またはplayerが 13 歳以上で、かつparentalconsent(保護者の同意)がある- 以下の条件を満たす場合のみ true となるブーリアン型変数
canAccessBonusを宣言します: playerがcanPlayGameであり、かつtutorialを完了している- 正確に以下のフォーマットで
resultsをPrintしてください:
Player age: 15
Has parental consent: true
Completed tutorial: false
Can play game: true
Can access bonus content: false出力はこの正確なフォーマットに一致する必要があります。
自分で試してみよう
void main() {
// ここで変数を宣言してください
// プレースホルダーの値を、タスクで求められているものに置き換えてください
int age = 0;
bool hasParentalConsent = false;
bool hasCompletedTutorial = false;
// プレイヤーがゲームをプレイできるか確認する
// プレースホルダーの年齢を正しい閾値に置き換えてください
bool canPlayGame = age >= 0 || (age >= 0 && hasParentalConsent);
// Check if player can access bonus content
// プレースホルダーの値を、タスクで説明されている条件に置き換えてください
bool canAccessBonus = true;
// 結果を出力する
print("Player age: $age");
print("Has parental consent: $hasParentalConsent");
print("Completed tutorial: $hasCompletedTutorial");
print("Can play game: $canPlayGame");
print("Can access bonus content: $canAccessBonus");
}このレッスンには短いクイズがあります。レッスンを始めて解答し、進捗を記録しましょう。
基礎のすべてのレッスン
自分で練習してみよう: Dartオンラインコンパイラ