package myproject;
/**
*
* @author 李瑞琦
* 用for循环:
* 输入一个数,判断你输入的数是质数还是合数
*
*/
public class TestFor {
public static void main(String[] args) {
int num = 17;
boolean flag = test(num);
if (flag) {
System.out.println("您输入的"+num+"为质数");
}else {
System.out.println("您输入的"+num+"为合数");
}
}
public static boolean test(int num){
for (int i = 2; i < num-1; i++) {
if (num%i==0) {
return false;
}
}
return true;
}
}
for循环判定质数合数
最新推荐文章于 2022-09-01 17:46:53 发布