我不知道这段代码有什么问题,为什么它会产生错误:
变量isPrime可能尚未初始化
这是完整的代码:
import java.util.Scanner;
public class PrimeNumberTest {
//Program to test for prime numbers
public static void main(String[] args) {
Scanner prime = new Scanner(System.in);
int number;
boolean isPrime;
System.out.print("Please enter number: ");
number = prime.nextInt();
if (number < 2)
isPrime = false;
else{
for (int i = 2; i < number / 2; i++)
if (number % i == 0){
isPrime = false;
break;
}
}
if (isPrime)
System.out.println("Your number is a prime number");
else
System.out.println("Your number is not a prime number");
}
}
答案 0 :(得分:0)
boolean isPrime = false;
只需初始化isPrime
布尔值即可。