Java程序给出两个区间之间的素数

我需要一个程序来打印任意两个间隔之间的所有素数,然后打印两个间隔之间有多少个素数。


所以我有一个正在运行的代码,但它不会打印数字 2 并且我知道 2 是一个素数。它正在正确地执行其他所有操作。我尝试了一些其他可以打印 2 的代码,但如果我输入负数,它也会给出负数。


import java.util.Scanner;


class Main {


  public static void main(String args[]) {


    int first, last, flag = 0, i, j;


    Scanner scanner = new Scanner(System.in);


    System.out.print("\nEnter the lower bound : ");

    first = scanner.nextInt();

    System.out.print("\nEnter the upper bound : ");

    last = scanner.nextInt();

    System.out.println("The prime numbers in between the entered limits are :");


    int x = 0;

    for (i = first; i <= last; i++) {

      for (j = 2; j < i; j++) {

        if (i % j == 0) {

          flag = 0;

          break;

        } else {

          flag = 1;

        }

      }

      if (flag == 1) {

        x++;

        System.out.println(i + " ");

      }

    }

    System.out.println("Total number of prime numbers between " + first + " and " + last + " are " + x);

  }

}

因此,如果我输入 -5(上限)和 10(下限),它应该打印: 2 3 5 7 -5 到 10 之间的素数总数是 4


但它打印 3 5 7 -5 到 10 之间的素数总数是 3


守着一只汪
浏览 143回答 4
4回答

婷婷同学_

只需在循环之前添加以下几行for,它就会给出预期的输出:int x = 0;if (first<3) {&nbsp; &nbsp; System.out.println(2);&nbsp; &nbsp; x++;}您的更新程序将是:import java.util.Scanner;class Main {&nbsp; public static void main(String args[]) {&nbsp; &nbsp; int first, last, flag = 0, i, j;&nbsp; &nbsp; Scanner scanner = new Scanner(System.in);&nbsp; &nbsp; System.out.print("\nEnter the lower bound : ");&nbsp; &nbsp; first = scanner.nextInt();&nbsp; &nbsp; System.out.print("\nEnter the upper bound : ");&nbsp; &nbsp; last = scanner.nextInt();&nbsp; &nbsp; System.out.println("The prime numbers in between the entered limits are :");&nbsp; &nbsp; int x = 0;&nbsp; &nbsp; if (first<3) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(2);&nbsp; &nbsp; &nbsp; &nbsp; x++;&nbsp; &nbsp; }&nbsp; &nbsp; for (i = first; i <= last; i++) {&nbsp; &nbsp; &nbsp; for (j = 2; j < i; j++) {&nbsp; &nbsp; &nbsp; &nbsp; if (i % j == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; flag = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; flag = 1;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; if (flag == 1) {&nbsp; &nbsp; &nbsp; &nbsp; x++;&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(i + " ");&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; System.out.println("Total number of prime numbers between " + first + " and " + last + " are " + x);&nbsp; }}

阿波罗的战车

内循环忽略数字 2。 j < i => 2 < 2 为 false

慕村225694

您可以检查此代码(经过优化,因为它在大范围内运行得更快。在迭代所有数字之前循环返回)&nbsp;public static void main(String args[]) {&nbsp; &nbsp; &nbsp; &nbsp; int first;&nbsp; &nbsp; &nbsp; &nbsp; int last;&nbsp; &nbsp; &nbsp; &nbsp; Scanner scanner = new Scanner(System.in);&nbsp; &nbsp; &nbsp; &nbsp; System.out.print("\nEnter the lower bound : ");&nbsp; &nbsp; &nbsp; &nbsp; first = scanner.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; System.out.print("\nEnter the upper bound : ");&nbsp; &nbsp; &nbsp; &nbsp; last = scanner.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("The prime numbers in between the entered limits are :");&nbsp; &nbsp; &nbsp; &nbsp; int x = 0;&nbsp; &nbsp; &nbsp; &nbsp; for (int i = first; i <= last; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (isPrime(i)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(i + " ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Total number of prime numbers between " + first + " and " + last + " are " + x);&nbsp; &nbsp; }&nbsp; &nbsp; static boolean isPrime(int n)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (n <= 1) //less than 2 are not&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; if (n <=3) // 2 and 3 are prime&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; if (n % 2 == 0 || n % 3 == 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 5; i * i <= n; i = i + 6)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (n % i == 0 || n % (i + 2) == 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; }你不需要旗帜。

繁花如伊

尝试这个:import java.util.Scanner;public class Main {&nbsp; &nbsp; public static void main(String args[]) {&nbsp; &nbsp; &nbsp; &nbsp; int i, n;&nbsp; &nbsp; &nbsp; &nbsp; int num;&nbsp; &nbsp; &nbsp; &nbsp; int maxCheck;&nbsp; &nbsp; &nbsp; &nbsp; boolean isPrime = true;&nbsp; &nbsp; &nbsp; &nbsp; String primeNumbersFound = "";&nbsp; &nbsp; &nbsp; Scanner sc=new Scanner(System.in);&nbsp; &nbsp; &nbsp; System.out.println("Enter the first number: ");&nbsp; &nbsp; &nbsp; &nbsp; num = sc.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Enter the second number: ");&nbsp; &nbsp; &nbsp; &nbsp; maxCheck= sc.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; for (i = num; i <= maxCheck; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; isPrime = CheckPrime(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (isPrime) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; primeNumbersFound = primeNumbersFound + i + " ";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Prime numbers from " + num + " to " + maxCheck + " are:");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(primeNumbersFound);}public static boolean CheckPrime(int n) {&nbsp; &nbsp; int remainder;&nbsp; &nbsp; for (int i = 2; i <= n / 2; i++) {&nbsp; &nbsp; &nbsp; &nbsp; remainder = n % i;&nbsp; &nbsp; &nbsp; &nbsp; if (remainder == 0) {&nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java