Java 我的程序出了什么问题?我试图找到从n到m的素数列表
我试图写一个程序来寻找从n到m的素数,但我不知道我做错了什么 例如,如果我输入2表示n,输入9表示m,我就返回Java 我的程序出了什么问题?我试图找到从n到m的素数列表,java,Java,我试图写一个程序来寻找从n到m的素数,但我不知道我做错了什么 例如,如果我输入2表示n,输入9表示m,我就返回 2 正确的输出应该是什么时候 2 3 5 7 这是我的密码: publicstaticvoidmain(字符串[]args) { 扫描仪输入=新扫描仪(System.in); PrintStream输出=System.out; 打印(“输入要测试的数字:”); int n=input.nextInt(); int m=input.nextInt(); 布尔值isPrime=true
2
正确的输出应该是什么时候
2
3
5
7
这是我的密码:
publicstaticvoidmain(字符串[]args)
{
扫描仪输入=新扫描仪(System.in);
PrintStream输出=System.out;
打印(“输入要测试的数字:”);
int n=input.nextInt();
int m=input.nextInt();
布尔值isPrime=true;
int i=2;
而(n>0&&n
我以自己的方式实现了它。希望这有帮助
import java.util.Scanner;
class PrimeNumbers
{
public static void main (String[] args)
{
Scanner scanner = new Scanner(System.in);
int i =0;
int num =0;
//Empty String
String primeNumbers = "";
System.out.println("Enter the value of n:");
int n = scanner.nextInt();
System.out.println("Enter the value of m:");
int m = scanner.nextInt();
for (i = n; i <= m; i++)
{
int counter=0;
for(num =i; num>=1; num--)
{
if(i%num==0)
{
counter = counter + 1;
}
}
if (counter ==2)
{
//Appended the Prime number to the String
primeNumbers = primeNumbers + i + " ";
}
}
System.out.println("Prime numbers from m to n are :");
System.out.println(primeNumbers);
}
}
import java.util.Scanner;
类素数
{
公共静态void main(字符串[]args)
{
扫描仪=新的扫描仪(System.in);
int i=0;
int num=0;
//空字符串
字符串素数=”;
System.out.println(“输入n的值:”);
int n=scanner.nextInt();
System.out.println(“输入m的值:”);
int m=scanner.nextInt();
对于(i=n;i=1;num--)
{
如果(i%num==0)
{
计数器=计数器+1;
}
}
如果(计数器==2)
{
//将素数追加到字符串
素数=素数+i+“”;
}
}
System.out.println(“从m到n的素数是:”);
System.out.println(素数);
}
}
我在这里没有看到prime find算法,我想看看其他人是否可以帮我做功课如果你不知道怎么了,当我输入2和9时,程序显示输出2,但它应该显示输出2 3 5 7thanx nikhilk进行编码,但我想知道我的编码有什么问题,它检查的是素数,为什么它不增加n的值,这就是问题所在。如果你只是给出一个开箱即用的解决方案,你并没有帮助OP真正解决他们代码的问题。