3章7

このJavaのプログラムは、変数abcに対して数学的な計算を行い、結果を表示するものです。問題のコードは次のとおりです。

public class Main { public static void main(String[] args) { int a = 100, b = 20, c = 30; System.out.println(a % b * c + a / b); }
}
Enter fullscreen mode
Exit fullscreen mode

このコードを理解するために、ステップバイステップで計算を見ていきましょう。

  1. a % bab で割った余りです。つまり、100 % 20 は 0 になります。
  2. c30 を代入しています。
  3. a / bab で割った商です。つまり、100 / 20 は 5 です。
  4. 以上の結果から、a % b * c0 * 30 となり、これは 0 です。
  5. 最後に、0a / b の結果である 5 を足します。

したがって、プログラムは 5 を出力します。これは選択肢Aが正しい理由です。数学的な演算では、乗算と除算は加算と減算よりも優先されますが、この場合は % 演算(余りを求める演算)が最初に行われてから、その結果に c が乗算され、最後に a / b の結果が加算されます。


Discover more from Coursity

Subscribe to get the latest posts sent to your email.

Leave a Comment

Your email address will not be published. Required fields are marked *

Discover more from Coursity

Subscribe now to keep reading and get access to the full archive.

Continue reading

Scroll to Top
Discover expert tips to secure smart home on public wi fi. Jamb 2025 sure expo /sure jamb 2025 sure questions and answers assistance. Empathy : the transformative power of understanding life skills.