java 定义并调用函数prime(m) 统计并输出m和n之间的素数的个数以及这些素数的和
输入一个正整数repeat(0<repeat<10),做repeat次下列运算:输入2个正整数m和n(1<=m,n<=500),统计并输出m和n之间的素数的个数以及这些素...
输入一个正整数repeat (0<repeat<10),做repeat次下列运算:
输入2 个正整数m和n(1<=m,n<=500),统计并输出m 和n之间的素数的个数以及这些素数的和(素数就是只能被1和自身整除的正整数,1不是素数,2是素数)。
要求定义并调用函数prime(m)判断m是否为素数。
例:括号内是说明
输入:
3 (repeat=3)
1 10 (m=1, n=10)
20 35 (m=20, n=35)
14 16 (m=14, n=16)
输出:
count=4, sum=17 (1到10之间有4个素数:2,3,5,7)
count=3, sum=83 (20到35之间有3个素数:23, 29, 31)
count=0, sum=0 (14到16之间没有素数)
import java.util.Scanner;
public class Test50005{
public static void main(String args[]){
int ri, repeat;
int count, i, m, n, sum;
Scanner in=new Scanner(System.in);
repeat=in.nextInt();
for(ri=1; ri<=repeat; ri++){
m=in.nextInt();
n=in.nextInt();
/*---------*/
System.out.println("count="+count+", sum="+sum);
}
}
/*------------*/
} 展开
输入2 个正整数m和n(1<=m,n<=500),统计并输出m 和n之间的素数的个数以及这些素数的和(素数就是只能被1和自身整除的正整数,1不是素数,2是素数)。
要求定义并调用函数prime(m)判断m是否为素数。
例:括号内是说明
输入:
3 (repeat=3)
1 10 (m=1, n=10)
20 35 (m=20, n=35)
14 16 (m=14, n=16)
输出:
count=4, sum=17 (1到10之间有4个素数:2,3,5,7)
count=3, sum=83 (20到35之间有3个素数:23, 29, 31)
count=0, sum=0 (14到16之间没有素数)
import java.util.Scanner;
public class Test50005{
public static void main(String args[]){
int ri, repeat;
int count, i, m, n, sum;
Scanner in=new Scanner(System.in);
repeat=in.nextInt();
for(ri=1; ri<=repeat; ri++){
m=in.nextInt();
n=in.nextInt();
/*---------*/
System.out.println("count="+count+", sum="+sum);
}
}
/*------------*/
} 展开
展开全部
lz,我稍微动了下你的代码:
public static void main(String args[]) {
int ri, repeat;
int i, m, n;
System.out.print("请输入执行次数:");
Scanner in=new Scanner(System.in);
repeat=in.nextInt();
for(ri=1; ri<=repeat; ri++){
int count=0,sum=0;
System.out.println("请指定范围:"+"\n"+"从:");
m=in.nextInt();
System.out.println("到:");
n=in.nextInt();
System.out.println(m+"-------------"+n+"之间的素数有:"+"\n");
for(int j=m;j<=n;j++)
{
if(prime(j)!=-1)
{
System.out.println(j);
count++;
sum+=j;
}
}
System.out.println("count="+count+", sum="+sum);
}
}
/*
* 这个是检测是不是素数的方法
*/
static int prime(int m)
{
int i=0,j;
for (j = 2; j <= m/2; j++) {
if (m % j == 0)
break;
}
if (j > m/2&&m!=1){
i=m;
}else{
i=-1;
}
return i;
}
public static void main(String args[]) {
int ri, repeat;
int i, m, n;
System.out.print("请输入执行次数:");
Scanner in=new Scanner(System.in);
repeat=in.nextInt();
for(ri=1; ri<=repeat; ri++){
int count=0,sum=0;
System.out.println("请指定范围:"+"\n"+"从:");
m=in.nextInt();
System.out.println("到:");
n=in.nextInt();
System.out.println(m+"-------------"+n+"之间的素数有:"+"\n");
for(int j=m;j<=n;j++)
{
if(prime(j)!=-1)
{
System.out.println(j);
count++;
sum+=j;
}
}
System.out.println("count="+count+", sum="+sum);
}
}
/*
* 这个是检测是不是素数的方法
*/
static int prime(int m)
{
int i=0,j;
for (j = 2; j <= m/2; j++) {
if (m % j == 0)
break;
}
if (j > m/2&&m!=1){
i=m;
}else{
i=-1;
}
return i;
}
展开全部
import java.util.Scanner;
public class Test50005 {
public static void main(String args[]) {
int ri, repeat;
int count, i, m, n, sum;
Scanner in = new Scanner(System.in);
repeat = in.nextInt();
for (ri = 1; ri <= repeat; ri++) {
m = in.nextInt();
n = in.nextInt();
m(m, n);
}
}
private static void m(int m, int n) {
// TODO Auto-generated method stub
int total = 0;
int sum = 0;
boolean f = true;
if (m == 1) {
m = 2;
}
for (int i = m; i <= n; i++) {
f = true;
for (int j = 2; j <= i / 2; j++) {
if (i % j == 0) {
f = false;
break;
}
}
if (f == true) {
total++;
sum += i;
System.out.println(i);
}
}
System.out.println("count=" + total + "," + "sum=" + sum);
}
}
public class Test50005 {
public static void main(String args[]) {
int ri, repeat;
int count, i, m, n, sum;
Scanner in = new Scanner(System.in);
repeat = in.nextInt();
for (ri = 1; ri <= repeat; ri++) {
m = in.nextInt();
n = in.nextInt();
m(m, n);
}
}
private static void m(int m, int n) {
// TODO Auto-generated method stub
int total = 0;
int sum = 0;
boolean f = true;
if (m == 1) {
m = 2;
}
for (int i = m; i <= n; i++) {
f = true;
for (int j = 2; j <= i / 2; j++) {
if (i % j == 0) {
f = false;
break;
}
}
if (f == true) {
total++;
sum += i;
System.out.println(i);
}
}
System.out.println("count=" + total + "," + "sum=" + sum);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询