英文:
Why is eclipse showing it is a dead code?
问题
class PrimeNo {
public static void main(String[] args) {
int n = 8; // 假设为任意随机数字
int i;
if (n == 0 || n == 1)
System.out.println("不是质数");
else {
for (i = 2; i < n; i++) {
if (n % i == 0) {
System.out.println("不是质数");
break;
}
}
if (i == n)
System.out.println("是质数");
}
}
}
我写了一个基本的代码来判断给定的数字是否为质数。我尝试了在代码中不使用标志位。我们假设 "n" 是任意数字。当我运行代码时,它会在不同的行上打印出 "是质数" 和 "不是质数"。另外,为什么这段代码被称为死代码?如果我们仔细看逻辑,这段代码应该可以正常运行。请帮我解决问题!
英文:
class PrimeNo {
public static void main(String[] args) {
int n = 8;// assumed to be any random number
int i;
if (n == 0 || n == 1)
System.out.println("Not a prime number");
else {
for (i = 3; i < n; i++) {
if (n % i == 0) {
System.out.println("Not a prime number");
} else {
System.out.println("Prime number");
}
break;
}
}
}
}
I have written a basic code to find a given number is Prime number or not? I tried this by not using flags. Let we assume "n" to be any number. When I run the code it prints both "Prime" and "Not Prime" in different lines. Also, why is this code a dead code? If we go through logic this code should have runned perfectly. Help me out guys!!!
答案1
得分: 2
你的 break
语句是问题所在,它会在第一次迭代时就中断循环,i
永远不会被增加,因此出现了无法执行的代码。
英文:
Your break
is the culprit, it will break at first iteration, i
will never get incremented and hence dead code.
答案2
得分: 0
由于您在代码中使用了 'break;' 语句,for 循环只运行了一次。这意味着您的 for 循环中的 'i++' 部分从未被调用过,因此出现了死代码错误。
您可能希望将逻辑编写得类似于以下内容:
class PrimeNo {
public static void main(String[] args) {
int n = 8; // 假设为任意随机数
int i;
boolean isNumberPrime = true;
if (n == 0 || n == 1)
isNumberPrime = false;
for (i = 2; i < n; i++) {
if (n % i == 0) {
isNumberPrime = false;
}
}
if (isNumberPrime) {
System.out.println("数字是素数");
} else {
System.out.println("数字不是素数");
}
}
}
英文:
Since you are using 'break;' in your code, for loop was running only once. This means the 'i++' part in your for loop was never been called and hence the dead code error.
You might want to write your logic something similar to this:
class PrimeNo {
public static void main(String[] args) {
int n = 8;// assumed to be any random number
int i;
boolean isNumberPrime = true;
if (n == 0 || n == 1)
isNumberPrime = false;
for (i = 2; i < n; i++) {
if (n % i == 0) {
isNumberPrime = false;
}
}
if (isNumberPrime) {
System.out.println("Number is prime number");
} else {
System.out.println("Number is not prime number");
}
}
}
答案3
得分: 0
抱歉,我错过了不使用标志的部分。我们可以按照以下代码实现,不使用标志:
class PrimeNo {
public static void main(String[] args) {
int n = 8; // 假设为任意随机数
int i;
if (n == 0 || n == 1)
System.out.println("数字不是素数");
for (i = 2; n > 1 && i < n; i++) {
if (n % i == 0) {
System.out.println("数字不是素数");
break;
} else if (i == n - 1) {
System.out.println("数字是素数");
}
}
}
}
英文:
Sorry I missed the no flag part. We could implement the code as below with no flags:
class PrimeNo {
public static void main(String[] args) {
int n = 8; // assumed to be any random number
int i;
if (n == 0 || n == 1)
System.out.println("Number is not prime number");
for (i = 2; n > 1 && i < n; i++) {
if (n % i == 0) {
System.out.println("Number is not prime number");
break;
} else if (i == n - 1) {
System.out.println("Number is prime number");
}
}
}
}
答案4
得分: 0
我已经编写了一个基本代码来判断给定的数字是否为质数?我尝试了不使用标志来实现这一点。
以下是您可以在不使用“标志”的情况下执行的方法:
```java
public class Main {
public static void main(String[] args) {
int n = 8; // 假设是任意随机数字
int i;
if (n == 0 || n == 1)
System.out.println("不是质数");
else {
for (i = 2; i < n; i++) {
if (n % i == 0) {
System.out.println("不是质数");
break;
}
}
// 如果在整个循环中 n % i == 0 从未变为真,这意味着 n 是质数
if (i == n) {
System.out.println("质数");
}
}
}
}
输出:
不是质数
请注意,我从 2
开始循环,2
也是一个质数。
另一个重要的事情是,您无需一直除到 n
(即 i < n
),只需检查到 n
的平方根即可。参考 这里 了解更多信息。因此,一种高效的实现方式如下:
public class Main {
public static void main(String[] args) {
int n = 8; // 假设是任意随机数字
int i, sqrt = (int) Math.sqrt(n);
if (n == 0 || n == 1)
System.out.println("不是质数");
else {
for (i = 2; i <= sqrt; i++) {
if (n % i == 0) {
System.out.println("不是质数");
break;
}
}
// 如果在整个循环中 n % i == 0 从未变为真,这意味着 n 是质数
if (i > sqrt) {
System.out.println("质数");
}
}
}
}
输出:
不是质数
> 此外,为什么这段代码是无效代码?
当循环运行到 i = 3
时,它会被 break;
语句终止。因此,i++
永远没有机会运行,因此它是无效代码。
<details>
<summary>英文:</summary>
> I have written a basic code to find a given number is Prime number or
> not? I tried this by not using flags.
Given below is how you can do it without using a `flag`:
public class Main {
public static void main(String[] args) {
int n = 8;// assumed to be any random number
int i;
if (n == 0 || n == 1)
System.out.println("Not a prime number");
else {
for (i = 2; i < n; i++) {
if (n % i == 0) {
System.out.println("Not a prime number");
break;
}
}
// If n % i == 0 doesn't become true throughout the loop, it means n is prime
if (i == n) {
System.out.println("Prime number");
}
}
}
}
**Output:**
Not a prime number
Note that I have started the loop from `2` which is also a prime number.
Another important thing to consider is that you do not need to divide and check until `n` (i.e. `i < n`), checking till the square root of `n` is sufficient. Check [this][1] to learn more about it. Thus, an efficient way of doing it as follows:
public class Main {
public static void main(String[] args) {
int n = 8;// assumed to be any random number
int i, sqrt = (int) Math.sqrt(n);
if (n == 0 || n == 1)
System.out.println("Not a prime number");
else {
for (i = 2; i <= sqrt; i++) {
if (n % i == 0) {
System.out.println("Not a prime number");
break;
}
}
// If n % i == 0 doesn't become true throughout the loop, it means n is prime
if (i > sqrt) {
System.out.println("Prime number");
}
}
}
}
**Output:**
Not a prime number
> Also, why is this code a dead code?
When your loop runs for `i = 3`, it will be terminated by `break;` statement. This, `i++` will never get a chance to run and thus, it is a dead code.
[1]: https://en.wikipedia.org/wiki/Primality_test
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论