sinat_28203663 于 2015.06.05 17:43 提问

public class All {
public static void main(String[] args) {
int a[] = new int[200];
//将前200个数中的素数提取出来放入数组a中
for(int i = 1 ; i < 200 ; i ++){
boolean isPrime = true;
for(int k = 2 ; k < i ; k ++){
if( i % k == 0){
isPrime = false;
break ;
}
}
if(isPrime){
a[i-1] = i;
System.out.println(a[i-1]); //打印确认
}
}

``````    //  输入两个数 m，n  求第m个素数到底n个素数之间的素数和
Scanner small = new Scanner(System.in);
Scanner large = new Scanner(System.in);
int m = small.nextInt();
int n = large.nextInt();
int sums = 0 ;
int suml = 0;
int sum = 0;
for(int i = 0 ; i < m ; i ++){
sums += a[i];
System.out.print(a[i]+"*");
}
for(int i = 0 ; i < n ; i ++){
suml += a[i];
System.out.print(a[i]+" ");
}
sum = suml - sums;
System.out.println(sum);
}
``````

}

3个回答

tongyi55555   2015.06.05 17:53

``````public class All {
public static void main(String[] args) {
int a[] = new int[200];
int index = 0;
// 将前200个数中的素数提取出来放入数组a中
for (int i = 1; i < 200; i++) {
boolean isPrime = true;
for (int k = 2; k < i; k++) {
if (i % k == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
a[index++] = i;
System.out.println(a[index-1]); // 打印确认
}
}
// 输入两个数 m，n 求第m个素数到底n个素数之间的素数和
Scanner small = new Scanner(System.in);
Scanner large = new Scanner(System.in);
int m = small.nextInt();
int n = large.nextInt();
int sums = 0;
int suml = 0;
int sum = 0;
for (int i = 0; i < m; i++) {
sums += a[i];
System.out.print(a[i] + "*");
}
for (int i = 0; i < n; i++) {
suml += a[i];
System.out.print(a[i] + " ");
}
sum = suml - sums;
System.out.println(sum);
}
}
``````
danielinbiti      2015.06.05 17:52

`````` if(isPrime){//这里只有符合条件的才更新数组a
a[i-1] = i;//所以如果i是198是偶数，那a[197]就是0
``````

tongyi55555   2015.06.05 17:51

sinat_28203663 我懂了 谢谢
2 年多之前 回复