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

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

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

问题

class PrimeTernary {
    public static void main(String[] args) {
        int i, m;
        int n = 8;
        m = n / 2;
        String result;
        if (n == 0 || n == 1)
            System.out.println("不是质数");
        else
            for (i = 2; i <= m; i++) {
                result = (n % i == 0) ? "不是质数" : "质数";
                System.out.println(result);
            }
    }
}

代码中的问题是:

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

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

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

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:

确定