在两个区间之间给出素数的Java程序
我需要一个程序来打印任意两个区间之间的所有素数,然后打印两个区间之间有多少素数 所以我有一个运行的代码,但它不会打印数字2,我知道2是一个素数。其他一切都做得很好。我尝试了一些其他代码,可以打印2,但如果我输入负数,它也会给出负数在两个区间之间给出素数的Java程序,java,Java,我需要一个程序来打印任意两个区间之间的所有素数,然后打印两个区间之间有多少素数 所以我有一个运行的代码,但它不会打印数字2,我知道2是一个素数。其他一切都做得很好。我尝试了一些其他代码,可以打印2,但如果我输入负数,它也会给出负数 import java.util.Scanner; class Main { public static void main(String args[]) { int first, last, flag = 0, i, j; Scanner
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);
}
}
import java.util.Scanner;
班长{
公共静态void main(字符串参数[]){
int first,last,flag=0,i,j;
扫描仪=新的扫描仪(System.in);
System.out.print(“\n输入下限:”);
first=scanner.nextInt();
System.out.print(“\n输入上限:”);
last=scanner.nextInt();
System.out.println(“输入限制之间的素数为:”);
int x=0;
对于(i=first;i内循环,忽略数字2)。
j2<2为false只需在for
循环的之前添加以下行,它将为您提供预期的输出:
int x = 0;
if (first<3) {
System.out.println(2);
x++;
}
intx=0;
if(首先您可以检查此代码(优化,因为它在大范围内运行更快。循环在迭代所有数字之前返回)
publicstaticvoidmain(字符串参数[]){
int优先;
int last;
扫描仪=新的扫描仪(System.in);
System.out.print(“\n输入下限:”);
first=scanner.nextInt();
System.out.print(“\n输入上限:”);
last=scanner.nextInt();
System.out.println(“输入限制之间的素数为:”);
int x=0;
对于(inti=first;i尝试以下方法:
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
int i, n;
int num;
int maxCheck;
boolean isPrime = true;
String primeNumbersFound = "";
Scanner sc=new Scanner(System.in);
System.out.println("Enter the first number: ");
num = sc.nextInt();
System.out.println("Enter the second number: ");
maxCheck= sc.nextInt();
for (i = num; i <= maxCheck; i++) {
isPrime = CheckPrime(i);
if (isPrime) {
primeNumbersFound = primeNumbersFound + i + " ";
}
}
System.out.println("Prime numbers from " + num + " to " + maxCheck + " are:");
System.out.println(primeNumbersFound);
}
public static boolean CheckPrime(int n) {
int remainder;
for (int i = 2; i <= n / 2; i++) {
remainder = n % i;
if (remainder == 0) {
return false;
}
}
return true;
}
}
导入java.util.Scanner;
公共班机{
公共静态void main(字符串参数[]){
inti,n;
int-num;
int-maxCheck;
布尔值isPrime=true;
字符串primeNumbersFound=“”;
扫描仪sc=新的扫描仪(System.in);
System.out.println(“输入第一个数字:”);
num=sc.nextInt();
System.out.println(“输入第二个数字:”);
maxCheck=sc.nextInt();
对于(i=num;我需要学习使用调试器!只需做一个特例并检查i==2
步骤1:当您得到负输入时,首先,并立即,在执行任何其他操作之前,将其重写为2
。因为根据定义,没有小于2的素数。其次,如果您自己编写此代码,您应该能够解释a每行发生了什么及其原因。它基本上只有6行代码,所以手动运行它:拿笔和纸,只运行输入min=2和max=10时发生的事情。每行上i和j的起始值和结束值是什么?flag
做什么?这只需要几分钟的时间就可以计算出来,并让您发现pr问题很容易解决。代码运行时没有错误,只是没有打印2。当我使用打印2的条件时,它会弄乱我需要打印的其余素数。这非常有效,非常简单,但非常有效。我曾在for循环中尝试过类似的方法,但它只是给了我错误的输出。谢谢!我是该网站的新手所以我不知道如何将答案标记为接受,算出答案!谢谢!这一个给出了正确的素数,如果较低的输入为2或更高,但如果我以一个负数开始较低的区间,则包含负数。这一个给我的唯一问题是数字3无法打印,这也是一个素数。然而,我能够解决这个问题通过将打印2作为素数的一行调整为:if(n==2 | | n==3)//2为素数返回true;
public static void main(String args[]) {
int first;
int last;
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 (int i = first; i <= last; i++) {
if (isPrime(i)) {
x++;
System.out.println(i + " ");
}
}
System.out.println("Total number of prime numbers between " + first + " and " + last + " are " + x);
}
static boolean isPrime(int n)
{
if (n <= 1) //less than 2 are not
return false;
if (n <=3) // 2 and 3 are prime
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
int i, n;
int num;
int maxCheck;
boolean isPrime = true;
String primeNumbersFound = "";
Scanner sc=new Scanner(System.in);
System.out.println("Enter the first number: ");
num = sc.nextInt();
System.out.println("Enter the second number: ");
maxCheck= sc.nextInt();
for (i = num; i <= maxCheck; i++) {
isPrime = CheckPrime(i);
if (isPrime) {
primeNumbersFound = primeNumbersFound + i + " ";
}
}
System.out.println("Prime numbers from " + num + " to " + maxCheck + " are:");
System.out.println(primeNumbersFound);
}
public static boolean CheckPrime(int n) {
int remainder;
for (int i = 2; i <= n / 2; i++) {
remainder = n % i;
if (remainder == 0) {
return false;
}
}
return true;
}
}