Import java.util.*;
public static void Main (string[] args) {
Scanner sc=new Scanner (system.in);
System.out.println ("Please enter an integer greater than 0 M");//prime number is the remainder is 1 and its own positive integer
int m=sc.nextint ();
if (m==2| | M==1) {System.out.println ("you entered a prime number");} Exclude 1 and 2 This is a special number
Else if (m>2) {int count=0;//greater than 2 is normally the case
for (int i=2;i<m;i++) {//excludes 1 and itself according to the prime number definition
if (m%i==0) {//NOTE: If this is the direct output System.out.println ("This is not a
A prime number "will cause an error, because he will directly determine that the input is divided by 2 as a single decision, except for the composite,
is a prime number, which is wrong, so we have to introduce count to count, and a condition that satisfies composite is +1;);
count+=1;
}
}
if (count>0) {//Determines whether a match is met
System.out.println ("This is not a prime number");
}else{
System.out.println ("This is a prime number");
}
}
}
This article is from the "12160084" blog, please be sure to keep this source http://12170084.blog.51cto.com/12160084/1862230
Whether the Java base instance is a prime number with if and for