【发布时间】:2022-10-23 16:33:06
【问题描述】:
我试图计算唯一的素数,我能够打印唯一的素数但无法计数,我也不打算使用额外的数组? 找到素数后我的逻辑出错了!谁能帮我这个 ?
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Please enter your array limit");
int limit=sc.nextInt();
int array[]=new int[limit];
System.out.println("please enter your array elements ");
for(int i=0;i<limit;i++) {
array[i]=sc.nextInt();
}
int flag=0;
for(int i=0;i<limit;i++) {
int counter=0;
for (int j=1;j<=array[i];j++) {
if(array[i]%j==0) {
counter++;
}
}
if(counter==2) {
System.out.println(array[i]);
for(int k=i;k<limit;k++) {
if(array[i]!=array[k]) {
flag++;
}
}
}
}
System.out.println("Count of unique prime number: "+flag);
sc.close();
}
}
output:
Please enter your array limit
6
please enter your array elements
1 2 3 4 5 6
2
3
5
Count of unique prime number: 8
//expected output 3
【问题讨论】: