package Demo;
import java.util.Scanner;
public class Test2 {
public static void main(String[] args) {
//2.建立对象接收范围
Scanner sc = new Scanner(System.in);
//1.输入范围
System.out.println("找素数" + "请您输入范围:");
//3.开始输入
System.out.println("起始范围:");
int first = sc.nextInt();
System.out.println("终止范围:");
int last = sc.nextInt();
//5.使用方法输出素数
System.out.println("在" + first + "到" + last + "范围内的素数有:");
judge(first, last);
}
//创建判断素数的方法
public static void judge(int first, int last){
int count = 0;
for (int i = first; i < last; i++) {
//信号位,标记
boolean flag = true;
for (int j = 2; j < i/2; j++) {
if (i % j == 0){
flag = false;
break;
}
}
//4.输出素数
if (flag){
System.out.print(i + " ");
count++;
if (count % 5 == 0){
System.out.print("\n");
}
}
//换行方便查看
}
}
}
【Java练习】在输入范围内寻找素数
最新推荐文章于 2022-11-26 20:06:20 发布