LoginSignup
1
1

Ruby silverの模擬問題を抜粋して解説をしていきます。

下記コードの出力を4択から選ぶ問題になっています。

score = 50
battle = score < 70 ? "勝ち" : "負け"
p battle

1.“勝ち”
2.エラーになる
3.“負け”
4.true

まず、1行目で変数scoreに50を代入します。

score = 50

次に、2行目で三項演算子を使ってif文をスリムに書いています。
その結果をbattleという変数に代入してます

score < 70 ? "勝ち" : "負け"

通常のif文だとこのように書かれます。

if socore < 70
  "勝ち"
else
  "負け"
end

次に、3行目でbattleを出力してますので結果として“勝ち”が表示されます。
解答としては、1番になります。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1