public class Parse {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
System.out.println("请输入数字:");
String s = cin.next();
int num = Integer.parseInt(s);
boolean b = false;
for (int i = 2; i < num; i++) {
if (num % i == 0) {
b = true;
break;
}
}
if (b) {
System.out.println(num + "不是质数!");
} else {
System.out.println(num + "是质数!");
}
}
}
思路很清晰,将输入的数从2开始,如果能整除的话,这个数就不是质数了!望采纳,谢谢!
附上运行结果截图: