/* 【程序27】 题目:求100之内的素数 */ package test; import java.util.Scanner; public class test { public static boolean prime(int number) { boolean flag = true; int mid = (int)Math.sqrt(number); for(int i = 2; i< mid+1; i++) { if(number % i == 0) { flag = false; break; } } return flag; } public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("请输入一个100以内的数字:"); String letter = sc.next(); int number = Integer.valueOf(letter); if(number > 100 || number < 0) { System.out.println("输入错误,请重新来过"); return; } if(prime(number)) { System.out.println("YES"); } else { System.out.println("NO"); } sc.close(); } }
时间: 2024-12-25 01:19:26