Menu
Coddy logo textTech

論理 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');
}
challenge icon

チャレンジ

簡単

論理AND(&&)演算子を使用して、ゲームの利用資格チェッカーを作成してください:

  1. 値が 15 の整数型変数 age を宣言します
  2. 値が true のブーリアン型変数 hasParentalConsent を宣言します
  3. 値が false のブーリアン型変数 hasCompletedTutorial を宣言します
  4. 以下の条件を満たす場合のみ true となるブーリアン型変数 canPlayGame を宣言します:
    • player が 18 歳以上、または
    • player が 13 歳以上で、かつ parental consent(保護者の同意)がある
  5. 以下の条件を満たす場合のみ true となるブーリアン型変数 canAccessBonus を宣言します:
    • playercanPlayGame であり、かつ tutorial を完了している
  6. 正確に以下のフォーマットで resultsPrint してください:
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");
}
quiz icon腕試し

このレッスンには短いクイズがあります。レッスンを始めて解答し、進捗を記録しましょう。

基礎のすべてのレッスン

自分で練習してみよう: Dartオンラインコンパイラ