关于for循环,很奇怪,这东西天天都在用着,结果今天看到一份面试题,输入一个数,判断是不是素数,写了写
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
int m = sc.nextInt();
boolean ean =true;
//Math.sqrt 是调用Math类中的sqrt方法,求一个数的平方根
for(int i =2 ; i <= Math.sqrt(m); i++){
if(m%i ==0){
ean =false;
break;
}
}
if (ean){
System.out.println("YES");
}else{
System.out.println("NO");
}
}
for(单次表达式;条件表达式;末尾循环体){中间循环体;}
条件表达式,如果成立,即进入方法体,不成立直接就过去了