Java introduction classic examples (1)

Time:2021-2-18

Java introduction classic examples (1)

1、 According to the age, the people who print out the current age are teenagers (below 18), young people (19-28), middle-aged (29-55) and old people (above 56).
Upper code

`import java.util.Scanner;

public class TextDemo {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println ("please enter the age to be judged");
        int year = scanner.nextInt();
        if(year < 18){
            System.out.println ("you are now a teenager");
        }
        if(year > 19 && year <28){
            System.out.println ("you are a young man now");
        }
        if(year > 29 && year <55) {
            System.out.println ("you're middle-aged now");
        }
        if(year > 56){
            System.out.println ("you are old now");
        }
    }
}` 

*   1
*   2
*   3
*   4
*   5
*   6
*   7
*   8
*   9
*   10
*   11
*   12
*   13
*   14
*   15
*   16
*   17
*   18
*   19
*   20
*   21

The results are as follows
Java introduction classic examples (1)

2、 Determine whether a number is prime.
Upper code

`import java.util.Scanner;

public class TextDemo {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println ("please enter the number to be judged");
        int a = scanner.nextInt();
        if(a == 1 || a == 2 ){
            System.out.println (the number is also a prime number));
        }else {
            int i = 2 ;
            while(i<a){
                if (a % i == 0 ){
                    System.out.println ("the number is not prime");
                    break;
                }
                i++;
            }
            if( a == i ){
                System.out.println ("the number is prime");
            }
        }
        
    }
 }` 

*   1
*   2
*   3
*   4
*   5
*   6
*   7
*   8
*   9
*   10
*   11
*   12
*   13
*   14
*   15
*   16
*   17
*   18
*   19
*   20
*   21
*   22
*   23
*   24
*   25

The results are as follows
Java introduction classic examples (1)

3、 Print all prime numbers between 1 and 100.
Upper code

`public class TextDemo {
    public static void main(String[] args) {
        System.out.println (the prime number between 1 and 100 is);
        for(int a = 2; a <=100 ; a++ ){
            int b = 2;
            while ( a % b != 0){
                b++;
            }
            if ( a == b){
                System.out.printf("%dt",a);
            }
        }
    }
}` 

*   1
*   2
*   3
*   4
*   5
*   6
*   7
*   8
*   9
*   10
*   11
*   12
*   13
*   14

The results are as follows
Java introduction classic examples (1)

4、 Output all leap years from 1980 to 2020

`public class TextDemo {
    public static void main(String[] args) {
        System.out.println (leap year has);
        int year = 1980;
        for (year = 1980; year <= 2020; year++){
            if (year % 100 == 0){
                if (year % 400 == 0);{
                    System.out.printf("%dt", year );
            }
        }else{
            if (year % 4 == 0 ){
                System.out.printf("%dt", year );
            }
            }
        }
    }
}` 

*   1
*   2
*   3
*   4
*   5
*   6
*   7
*   8
*   9
*   10
*   11
*   12
*   13
*   14
*   15
*   16
*   17

The results are as follows
Java introduction classic examples (1)

5、 Output multiplication formula table
Code directly

`public class TextDemo {
    public static void main(String[] args) {
        int num = 1;
        for (int i = 1; i < 10;i++){
            for (int j = 1; j <= i ;j++){
                num = i * j ;
                System.out.print(j + "*" + i + "=" + num + " ");
                //The "+" here is not an addition operation, but a splicing operation. 
            }
            System.out.println();
        }
    }
}` 

*   1
*   2
*   3
*   4
*   5
*   6
*   7
*   8
*   9
*   10
*   11
*   12
*   13

The results are as follows
Java introduction classic examples (1)

6、 Find the greatest common divisor of two positive integers.
This is the most classic problem, we must master.

`import java.util.Scanner;

public class TextDemo {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println ("please enter two positive integers");
        int a = scanner.nextInt();
        int b = scanner.nextInt();
        for (int i =Math.min( a , b ); i >0; i--){
            if (a % i == 0 && b % i == 0){
                System.out.println (the greatest common divisor of these two positive integers is "+ I");
                break;
            }
        }
}` 

*   1
*   2
*   3
*   4
*   5
*   6
*   7
*   8
*   9
*   10
*   11
*   12
*   13
*   14
*   15

The results are as follows
Java introduction classic examples (1)

7、 Calculate 1 / 1-1 / 2 + 1 / 3-1 / 4 + 1 / 5 +1 / 99 – 1 / 100.
As long as the idea is clear, it is very simple. Find the rule, odd term is positive, even term is negative. Let’s go straight to the code.

`public class TextDemo {
    public static void main(String[] args) {
        double a = 0.0;
        double b = 0.0;
        double sum = 0.0;
        for (double i = 1;i <= 100;i += 2){
            a += 1 / i;
        }
        for (double i = 2;i <= 100;i += 2){
            b += 1 / i;
        }
        sum = a - b ;
        System.out.println(sum);
    }
}` 

*   1
*   2
*   3
*   4
*   5
*   6
*   7
*   8
*   9
*   10
*   11
*   12
*   13
*   14
*   15

The results are as follows
Java introduction classic examples (1)