大家下午好,
我目前正在尝试创建一个执行以下操作的程序:
开发一个代码,打印所有素数,直到输入的用户为止 数。输出的一个例子:
Enter an integer (2 or above): 19
The prime numbers up to you integer are:
2
3
5
7
11
13
17
19
不幸的是,还有一个"要求列表"这也需要得到满足:
对于这个程序,为了测试一个数字是否为素数,你应该尝试将数字除以2到数字1的每个值,看看它是否均匀分开。例如:
- 要查看5是素数还是: 5不均匀地除以2 5不均匀分为3 5不均匀分为4 因此5是素数
- 要查看9是否为素数: 9不均匀地除以2 9平均分为3 因此9不是素数
此程序要求您编写嵌套循环(即循环内的循环)。将使用一个循环从2到用户的数字进行计数,这样您就可以测试这些数字中的每个数字,看它是否为素数。对于这些数字中的每一个,x:
上面的问题是关于我目前的代码:
import java.util。*;
public class Something3 {
public static void main(String[] args) {
Scanner kbd = new Scanner(System.in);
//Variable declaration.
int limit, number;
//get input till which prime number to be printed
System.out.print("Enter an integer (2 or above): ");
limit = kbd.nextInt();
kbd.close();
//Will print prime numbers till the limit (user entered integer).
number = 2;
if (limit >=2) {
System.out.println("The prim numbers up to your interger are: " + limit+"\n");
for(int i = 0; i <= limit;){
//print prime numbers only
if(isPrime(number)){
System.out.println(number +"\n");
i++;
}
number = number + 1;
}
}
else
System.out.println("Number is not vaild");
}
//Prime number is not divisible by any number other than 1 and itself
//return true if number is prime.
public static boolean isPrime(int number){
for(int i=2; i==number; i++){
if(number%i == 0){
return false; //Number is divisible, thus not prime.
}
}
return true;//The number is prime.
}
}
极限变量I&#39; m是否使用此问题?任何帮助将非常感激。
答案 0 :(得分:3)
Your actual problem was number variable. Here's your solution. There's no need of variable number. Below is your solution
import java.util.*;
public class Something3 {
public static void main(String[] args) {
Scanner kbd = new Scanner(System.in);
// Variable declaration.
int limit;
// get input till which prime number to be printed
System.out.print("Enter an integer (2 or above): ");
limit = kbd.nextInt();
kbd.close();
if (limit >= 2) {
System.out.println("The prim numbers up to your interger are: "
+ limit + "\n");
for (int i = 1; i <= limit; i++) {
// print prime numbers only
if (isPrime(i)) {
System.out.println(i);
}
}
} else
System.out.println("Number is not vaild");
}
// Prime number is not divisible by any number other than 1 and itself
// return true if number is prime.
public static boolean isPrime(int n) {
if (n % 2 == 0)
// The only even prime is 2.
return (n == 2);
for (int i = 3; i * i <= n; i += 2) {
if (n % i == 0)
return false;
}
return true;
}
}
答案 1 :(得分:3)
your isPrime()
method is returning wrong result. for loop condition is wrong i==number
it should be i < number
public static boolean isPrime(int number){
for(int i=2; i*i <= number; i++){
if( number % i == 0){
return false; // Number is divisible, thus not prime.
}
}
return true; //The number is prime.
}
To check a number prime or not you don't have to check divisbility from 2 to N, all you need to check upto sqrt(N)
.
To find prime numbers in a range(N) use Sieve of Eratosthenes
答案 2 :(得分:0)
import java.util.Scanner;
class prime
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("enter the value of n");
int n=sc.nextInt();
for(int i=2;i<=n;i++)
{
int count=0;
for(int j=1;j<=i;j++)
{
if(i%j==0)
{
count++;
}
}
if(count==2)
{
System.out.println(i+" ");
}
}
}
}