为什么在使用了 system.out.println 函数后,Java 没有任何输出?

huangapple go评论103阅读模式
英文:

why there is no output in java even after using system.out.println function?

问题

  1. class PrimeTernary {
  2. public static void main(String[] args) {
  3. int i, m;
  4. int n = 8;
  5. m = n / 2;
  6. String result;
  7. if (n == 0 || n == 1)
  8. System.out.println("不是质数");
  9. else
  10. for (i = 2; i <= m; i++) {
  11. result = (n % i == 0) ? "不是质数" : "质数";
  12. System.out.println(result);
  13. }
  14. }
  15. }

代码中的问题是:

  1. 在输出结果时,应该使用中文字符串,而不是 System.out.println("&quot;Not prime number&quot;");,应改为 System.out.println("不是质数");
  2. 循环应从 i = 2 开始,因为质数定义为大于 1 且只能被 1 和自身整除的数。

以上是你的代码的翻译和修改部分。

英文:
  1. class PrimeTernary {
  2. public static void main(String[] args) {
  3. int i, m;
  4. int n = 8;
  5. m = n / 2;
  6. String result;
  7. if (n == 0 || n == 1)
  8. System.out.println(&quot;Not prime number&quot;);
  9. else
  10. for (i = 3; i &lt;= m; i++) {
  11. result = (n % i == 0) ? &quot;not prime&quot; : &quot;prime&quot;;
  12. System.out.println(result);
  13. }
  14. }
  15. }

what is wrong in my code? Can anyone pleased to explain it in brief?

答案1

得分: 1

即使 n 为 0 或 1(只有在这种情况下你的打印语句才会被执行),那么 m 必定为 0。这意味着 for 循环不会运行。

英文:

Even if n is 0 or 1 (only then your print statement could ever be reached), then m is necessarily 0. This means, the for loop does not run.

答案2

得分: 0

你的 if 条件从不成立,因为 n 总是 8。

if (n == 0 || n == 1)if (8 == 0 || 8 == 1) 是一样的。

所以你的循环永远不会执行。

英文:

Your if condition is never true since n is always 8.

if (n == 0 || n == 1) is the same as if (8 == 0 || 8 == 1)

So your loop will never execute.

huangapple
  • 本文由 发表于 2020年9月23日 19:39:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/64027134.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定