? ? ? ? 利用for循环,只要2~number-1范围内,任意一个数能够被number整除,就说明不是素数,直接循环暴力解决即可。????????
public static void main ( String [] args ) {????????// 5 2 3 4????????// 6 2 3 4 5????????// 7 2 3 4 5 6????????Scanner sc = new Scanner ( System . in );????????System . out . println ( " 请输入一个整数: " );????????int number = sc . nextInt ();????????boolean isPrime = true ; // 任何数都默认是素数????????for ( int i = 2 ; i < number ; i ++ ){????????????????//只要2~number-1 范围内,任意一个数能够被 number 整除,????????????????//则说明该数不是素数????????????????if ( number % i == 0 ){????????????????????????isPrime = false ;????????????????????????break ;????????}}????????if ( isPrime ){????????????????System . out . println ( number + " 是素数 " );????????} else {????????????????System . out . println ( number + " 是和数 " );????????}}
?
?
?