主要问题java

素数是一个大于 1 的正整数,它只能被它自己和 1 整除。在这个作业中,您将负责编写一个完整的 Java 程序来显示前 N 个素数。换句话说,你的程序应该列出前 N 个素数。


功能要求


您的程序应提示用户输入正数或值 -1 以终止程序。如果用户输入 0 或负数,程序也将立即结束。您的程序将显示用户给出的前 N 个素数。例如,如果用户输入 3,程序应该显示:“2, 3, 5”,它们是前三个质数。如果用户输入 6,输出将是:“2, 3, 5, 7, 11, 13”。


样品运行


Welcome to the list of N prime numbers program!

===============================================

Please enter the value of N (positive integer):

6


First 6 prime numbers are:

2

3

5

7

11

13

当我处理它时,我得到了这个,但需要帮助完成


import java.util.Scanner;


public class prime {

    public static void main(String[] args) {

        System.out.print("Welcome to the list of N prime numbers program! \n========================================================\nPlease enter the value of N (positive integer): ");

        Scanner scan = new Scanner(System.in);

        int n;

        int status=1;

        int num=3;

        n = scan.nextInt();

        if(n>=1) {

            System.out.println(2);

            for(int count=2; count<=n; count++) {

                for(int j=2; j<=Math.sqrt(num);j++) {

                    if(num%j==0) {

                        status =0;

                        break;

                    }

                    if(status!=0) {

                        System.out.println(num);

                        count++;

                    }

                }

                status=1;

                num++;

            }

        }

    }

}


森栏
浏览 137回答 3
3回答

摇曳的蔷薇

请使用以下方法获得素数。static List<Integer> nthPrimeNo(int nth){&nbsp; &nbsp; List<Integer> integers = new ArrayList<>();&nbsp; &nbsp; int num, count, i;&nbsp; &nbsp; num=2;&nbsp; &nbsp; count=0;&nbsp; &nbsp; for (int j = 0; j < nth; j++) {&nbsp; &nbsp; &nbsp; &nbsp; while (count < j){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; num=num+1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (i = 2; i <= num; i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (num % i == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( i == num){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count = count+1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; integers.add(num);&nbsp; &nbsp; }&nbsp; &nbsp; return integers;}或者你想得到比素数大 10 的数,static List<Integer> sieveOfEratosthenes(int n) {&nbsp; &nbsp; boolean prime[] = new boolean[n + 1];&nbsp; &nbsp; Arrays.fill(prime, true);&nbsp; &nbsp; for (int p = 2; p * p <= n; p++) {&nbsp; &nbsp; &nbsp; &nbsp; if (prime[p]) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = p * 2; i <= n; i += p) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; prime[i] = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; List<Integer> primeNumbers = new LinkedList<>();&nbsp; &nbsp; for (int i = 2; i <= n; i++) {&nbsp; &nbsp; &nbsp; &nbsp; if (prime[i]) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; primeNumbers.add(i);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return primeNumbers;}

白衣非少年

public class prime {public static boolean isPrime(int n) {&nbsp; &nbsp; for(int j=2; j<=Math.sqrt(n)+1;j++) { //Math.sqrt(n) + 1 because you want to check more than half of the original value.&nbsp; &nbsp; &nbsp; &nbsp; if(n%j==0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; return true;}public static void main(String[] args) {&nbsp; &nbsp; System.out.print("Welcome to the list of N prime numbers program! \n========================================================\nPlease enter the value of N (positive integer): ");&nbsp; &nbsp; Scanner scan = new Scanner(System.in);&nbsp; &nbsp; int inputNum; //try making the variable name meaningful&nbsp; &nbsp; inputNum = scan.nextInt();&nbsp; &nbsp; int count = 0;&nbsp; &nbsp; int startingVal = 2;&nbsp; &nbsp; while(count<inputNum) {&nbsp; &nbsp; &nbsp; &nbsp; if(inputNum==-1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if(count==0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(2);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count++;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else if(isPrime(startingVal)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(startingVal);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count++;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; startingVal ++;&nbsp; &nbsp; }}}这应该可以正常工作。正如评论部分中的@Amadan 已经提到的,您的程序无法运行,因为您的 if(status!=0) 处于 for 循环中。此外,设置有意义的变量名称有助于您更轻松地修复或编辑代码。

千万里不及你

你应该给出一个out.println("enter the number of prime numbers needed"); 然后使用扫描仪读取它(例如,如果它正在读入 x)并提供一个 if 条件作为if(x<=0){break;}和余额代码可以在else条件中给出。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java