論理 OR
CoddyのDartジャーニー「基礎」セクションの一部。レッスン 24/94。
論理OR演算子(||)は、少なくとも1つの式が true である場合に true を返し、両方が false である場合にのみ false を返します。
void main() {
bool hasCreditCard = false;
bool hasDebitCard = true;
bool canPayOnline = hasCreditCard || hasDebitCard;
print('Can this person pay online? $canPayOnline');
}出力: Can this person pay online? true
void main() {
int age = 16;
bool hasParentalConsent = true;
bool canWatchMovie = (age >= 18) || (age >= 13 && hasParentalConsent);
print('Can watch the movie? $canWatchMovie');
}チャレンジ
初心者論理OR(||)演算子を使用してゲームの利用資格チェッカーを作成します:
- 値が 15 の整数の変数
ageを宣言します - 値が true のブール変数
hasParentalConsentを宣言します - 次のいずれかをチェックするブール変数
canPlayGameを宣言します: personが 18 歳以上である(age >= 18)、またはpersonが保護者の同意を得ている(hasParentalConsent が true)- 正確に以下のフォーマットで結果を出力します:
Age: 15
Has parental consent: true
Can play game: true次に hasParentalConsent を false に変更し、同じフォーマットで更新された結果を出力します。
自分で試してみよう
void main() {
// ここに変数を宣言してください
// 各プレースホルダーの値を、タスクで説明されている値に置き換えてください
int age = 0;
bool hasParentalConsent = false;
// その人がゲームをプレイできるか確認する
bool canPlayGame = age >= 0 || hasParentalConsent;
// 初期結果を出力する
print("Age: $age");
print("Has parental consent: $hasParentalConsent");
print("Can play game: $canPlayGame");
// Change parental consent status
hasParentalConsent = true;
canPlayGame = age >= 0 || hasParentalConsent;
// 更新された結果を出力する
print("\nAge: $age");
print("Has parental consent: $hasParentalConsent");
print("Can play game: $canPlayGame");
}このレッスンには短いクイズがあります。レッスンを始めて解答し、進捗を記録しましょう。
基礎のすべてのレッスン
自分で練習してみよう: Dartオンラインコンパイラ