Calculates the smallest prime that is greater than one number, based on the input number.
Words are not much to say, direct paste code.
Package com.fuxuemingzhu.countprime.main;
Import Java.util.Scanner; /** * <p> * title:main * </p> * <p> * Description: Calculates a minimum prime number greater than an input of * </p> * * @author Fuxu Emingzhu * * * * * @email fuxuemingzhu@163.com * * @date October 27, 2014 PM 4:01:41/public class Countprime {/** * INP
Utnum Input integer */private static int inputnum = 0; /** * <p> * title:main * </p> * <p> * Description: The main function of the program, from here into the * </p> * * @param
args/public static void main (string[] args) {//guide for a guided input System.out.println ("Please enter an integer to be evaluated:");
Gets the keyboard input getinput ();
Output results (); /** * <p> * title:getinput * </p> * <p> * Description: Getting keyboard input Results * </p> * * PRIV
ate static void GetInput () {////Scanner class gets keyboard operation Scanner Inputscanner = new Scanner (system.in);
Inputnum = Inputscanner.nextint ();
Close Inputscanner inputscanner.close (); }/** * <p> * title:output *</p> * <p> * Description: Output operation, at the same time the input of the judgment * </p>/private static void output () {if (checkIn
Put (Inputnum)) {System.out.println ("the smallest prime that is greater than this number is:" + countprime (inputnum)); } else {System.out.println ("input error.") You need to enter a positive integer.
");
}/** * <p> * title:checkinput * </p> * <p> * Description: Need to enter a positive integer * </p> *
* @param num * @return input is correct/private static Boolean checkinput (int num) {if (num >= 1) {return true;
return false;
/** * <p> * title:countprime * </p> * <p> * Description: Calculate the smallest prime number greater than num * </p> * * @param num * @return The smallest prime number greater than num/private static int countprime (int num) {/////////////////is greater than the smallest prime int
Answer = num + 1;
The above +1 is judged from the next number of this number, and does not judge whether the number itself is a prime while (IsPrime (answer)) {answer++;
return answer;
/** * <p> * title:isprime * </p> * <p> * Description: Determining whether a number is prime * </p> * * @param num * @return A number is not prime */private static Boolean isprime (int num) {Boolean check = false;
Calculates the square root of this number, reducing the computational steps for (int i = 2; I <= math.sqrt (num); i++) {///////to determine whether the remainder of the divisible variable i is 0, zero indicates that it is divisible, not prime
if (num% (int) i) = = 0) {return true;
} return check;
}
}
Attach a screenshot of the operation.