package com.imooc;
import java.util.Scanner;
public class Sushu {
int flag = 0;
Scanner input = new Scanner(System.in);
public void s(){
System.out.println("求二个数之间的素数,请输入二个数");
}
int num1 = input.nextInt();
int num2 = input.nextInt();{
if(num1 > num2){
int num3 = num1;
num1 = num2;
num2 = num3;
}
for(; num1<num2;num1++){
flag = 0;
for(int i=2;i<num1;i++){
if(num1%i==0)
flag = 1;
}
if(flag != 1 && num1 != 1)
System.out.println(num1);
}
}
}package com.imooc;
public class Initail {
public static void main(String[] args) {
Sushu su = new Sushu();
su.s();
}
}请我问为什么我先输入1,20出现2,3,5,7,11,13,17,19后出现提示信息“求二个数之间的素数,请输入二个数”。package com.imooc;
import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("求二个数之间的素数,请输入二个数:");
int num1 = input.nextInt();
int num2 = input.nextInt();
int flag = 0;
if(num1>num2){
int num3 = num1;
num1 = num2;
num2 = num3;
}
for(; num1<num2; num1++){
flag = 0;
for(int i=2;i<num1;i++){
if(num1%i==0)
flag = 1;
}
if(num1!=1 && flag != 1)
System.out.println(num1);
}
}
}这样改后对了,但有把所有工作放在了主函数中。老师说主函数只调用,最好不做其它工作。
添加回答
举报
0/150
提交
取消