英文:
Check whether the given number is prime or not?
问题
这是我的代码。
Scanner scan = new Scanner(System.in);
int number = scan.nextInt();
int[] arr = new int[number];
for (int i = 0; i < number; i++) {
arr[i] = scan.nextInt();
}
for (int i = 0; i < number; i++) {
int num = arr[i];
for (int j = 2; j <= Math.sqrt(num); j++) {
if (num % j == 0 && num != 2) {
System.out.println(num + "不是质数");
break;
}
}
System.out.println(num + "是质数");
}
英文:
I am trying to implement a code to check whether a given array of numbers is prime or not,
but when the number is not a prime number, the output displays "Prime" and "Not Prime" both answers. What is the mistake I did here and it is a pleasure to have an answer from you? Thank you in advance!
Here is my code.
Scanner scan = new Scanner(System.in);
int number = scan.nextInt();
int[] arr = new int[number];
for (int i = 0; i < number; i++) {
arr[i] = scan.nextInt();
}
for (int i = 0; i < number; i++) {
int num = arr[i];
for (int j = 2; j <= Math.sqrt(num); j++) {
if (num % j == 0 && num !=2) {
System.out.println(num + "Not prime");
break;
}
}
System.out.println(num +"Prime");
}
答案1
得分: 2
如果你对使你的代码更高效感兴趣,可以采用以下方法。
int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20 };
for (int num : numbers) {
System.out.println(num + ((isPrime(num) ? " 是" : " 不是") + " 质数"));
}
private static boolean isPrime(int num) {
// 两是质数
if (num == 2) {
return true;
}
// 数字小于等于1或任何偶数
// 数字(除2外)都不是质数
if (num <= 1 || num % 2 == 0) {
return false;
}
// 现在你可以检查奇数除数。
// 并从3开始每次增加2。
for (int i = 3; i <= Math.sqrt(num); i+=2) {
if (num % i == 0) {
return false;
}
}
return true;
}
英文:
If you're interested in making your code a little more efficient you can go this route.
int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20 };
for (int num : numbers) {
System.out.println(num + ((isPrime(num) ? " is" : " is not") + " a prime"));
}
private static boolean isPrime(int num) {
// two is a prime
if (num == 2) {
return true;
}
// numbers 1 or less or any even
// number (sans 2) are not primes
if (num <= 1 || num % 2 == 0) {
return false;
}
// Now you can check for odd divisors.
// and increment by 2 starting with 3.
for (int i = 3; i <= Math.sqrt(num); i+=2) {
if (num % i == 0) {
return false;
}
}
return true;
}
答案2
得分: 1
你应该记住这个数字是不是素数。你的代码没有这样做,因此两个打印语句都会被执行。
Scanner scan = new Scanner(System.in);
int number = scan.nextInt();
int[] arr = new int[number];
for (int i = 0; i < number; i++) {
arr[i] = scan.nextInt();
}
for (int i = 0; i < number; i++) {
int num = arr[i];
boolean isPrime = true;
for (int j = 2; j <= Math.sqrt(num); j++) {
if (num % j == 0 && num != 2) {
isPrime = false;
break;
}
}
if (isPrime) {
System.out.println(num + "是素数");
} else {
System.out.println(num + "不是素数");
}
}
英文:
You should remember whether the number was prime or not. Your code doesn't do that so both prints are reached.
Scanner scan = new Scanner(System.in);
int number = scan.nextInt();
int[] arr = new int[number];
for (int i = 0; i < number; i++) {
arr[i] = scan.nextInt();
}
for (int i = 0; i < number; i++) {
int num = arr[i];
boolean isPrime = true;
for (int j = 2; j <= Math.sqrt(num); j++) {
if (num % j == 0 && num !=2) {
isPrime = false;
break;
}
}
if (isPrime) {
System.out.println(num +"Prime");
} else {
System.out.println(num + "Not prime");
}
}
答案3
得分: 1
使用布尔值来跟踪数字是否为素数。开始时假设它为true(素数),如果发现不是素数,则将其设置为false。
boolean isPrime = true;
然后,根据该布尔值确定消息。
String message = isPrime ? "素数" : "非素数";
英文:
Use a boolean to track whether the number is prime. Assume it's true (prime) to begin with and set it false if it is discovered not to be prime.
boolean isPrime = true;
Afterwards, determine the message based on that boolean.
String message = isPrime ? "Prime" : "Not prime";
答案4
得分: 0
你可以通过引入一个布尔变量来修复它,例如:
for (int i = 0; i < number; i++) {
int num = arr[i];
boolean isPrime = true;
for (int j = 2; j <= Math.sqrt(num); j++) {
if (num % j == 0 && num != 2) {
isPrime = false;
break;
}
}
if (isPrime) {
System.out.println(num + "是质数");
} else {
System.out.println(num + "不是质数");
}
}
英文:
You can fix it for example by introducing a boolean variable:
for (int i = 0; i < number; i++) {
int num = arr[i];
boolean isPrime = true;
for (int j = 2; j <= Math.sqrt(num); j++) {
if (num % j == 0 && num !=2) {
isPrime = false;
break;
}
}
if (isPrime) {
System.out.println(num +"Prime");
} else {
System.out.println(num + "Not prime");
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论