Title: Output a prime number from three to 100, output a prime number between any two integers, and output a maximum and a minimum of 10 primes between any two integers.
Code:
public class Ketang {
public static void Main (String args[]) throws Exception {
int number=0; For counting output
The number of primes between 3 and 100 is output
System.out.println ("The prime number between three to 100 is as follows:");
for (int i=3;i<98;i++) {
if (prime (i) ==0) {
number++;
System.out.print (i+ "");
}
if (number==5) {
System.out.println ();
number=0;
}
}
System.out.println ();
Outputs a prime number between any two integers
System.out.println ("The number of primes between any two primes");
System.out.print ("Please enter the first integer:");
int N1=in_enter (); Invoke Input Method
System.out.print ("Please enter a second integer:");
int N2=in_enter ();
number=0; Re-count
int d1=n1>n2?n1:n2; Judging size
int x1=n1<n2?n1:n2;
int p=0; Used to determine whether the number of primes between two numbers is 0
System.out.println ("The prime number between two numbers is:");
for (int i=x1;i<=d1;i++) {
if (prime (i) ==0) {
number++;
P=1;
System.out.print (i+ "");
}
if (number==5) {
System.out.println ();
number=0;
}
}
if (p==0)
System.out.print ("Sorry, judging, there is no prime number between two numbers!") ");
System.out.println ();
Maximum and minimum 10 numbers in the middle of a two-digit output
SYSTEM.OUT.PRINTLN ("Output two number of middle largest and smallest 10 primes");
System.out.print ("Please enter the first integer:");
int N3=in_enter ();
System.out.print ("Please enter a second integer:");
int N4=in_enter ();
int d2=n3>n4?n3:n4; Judging size
int x2=n3<n4?n3:n4;
int j=0; Array subscript of prime number
int sa[]=new int[10]; Array of prime numbers
for (int i=0;i<10;i++) {//Assign initial value
sa[1]=0;
}
for (int i=x2;i<=d2;i++) {//min 10
if (prime (i) ==0) {
Sa[j]=i; Array Assignment
j + +; Each assignment succeeds one, subscript plus 1
}
if (j==10)
Break
}
OutPut (J,SA); Calling the Output method
j=0; Subscript is 0, start counting again
for (int i=d2;i>=x2;i--) {//MAX 10
if (prime (i) ==0) {
Sa[j]=i; Array Assignment
j + +; Each assignment succeeds one, subscript plus 1
}
if (j==10)
Break
}
OutPut (J,SA); Calling the Output method
}
Judging prime numbers
public static int prime (int n) {
int is_prime=1; For output judgment
if (n==2)//2 is prime
return is_prime=0;
Else
for (int i=2;i<=n/2+1;i++)//To determine whether it is a prime number
if (n%i = = 0)
return is_prime=1;
return is_prime=0;
}
Input Method
public static int In_enter () {
String enter= "";
int num;
int n=1;
while (n==1) {
try{
BufferedReader in=new BufferedReader (new
InputStreamReader (system.in));
Enter=in.readline ();
Char[] Ca=enter.tochararray (); Convert to char array to determine whether the input is legal
for (int i=0;i<ca.length;i++) {//judgment, if the input is unreasonable, re-enter
if (ca[i]< ' 0 ' | | Ca[i]> ' 9 ') {//judgment
N=1;
System.out.print ("The input is illegal! Please re-enter: ");
Break
}
Else
n=0;
}
}catch (IOException e) {}
}
Return Num=integer.parseint (enter);
}
Output method
public static void OutPut (int b,int s[]) {
int a=0; Recalculate the number of rows per row
if (b<10) {//two number of intermediates less than 10
if (b==0)//Two number middle no prime
System.out.println ("The number of primes in the middle of two numbers is 0!") ");
else{
System.out.println ("The middle of the two number is only" +b+ "primes, these primes are");
for (int i=0;i<b;i++) {
System.out.print (s[i]+ "");
a++;
if (a==5) {//five outputs per line
System.out.println ();
a=0;
}
}
}
}
else{
System.out.println ("These primes are:" respectively);
for (int i=0;i<10;i++) {
System.out.print (s[i]+ "");
a++;
if (a==5) {//five outputs per line
System.out.println ();
a=0;
}
}
}
}
}
Places to consider: 1) 2 is a prime number, which is interpreted separately in the method of judging primes. 2) The second case should be considered when the number of primes is zero, and the other output is zero.
3) The third case considers that the number of primes is zero or less than 10. 4) Input method should be added to determine the input code, so that the program automatically error, rather than the system error.
5) Java compared to C + +, many do not know the method, perhaps the application of these methods, can reduce the amount of code.
Java Prime judgment