Examples of Java basic syntax

Keywords: Java

Example 1:
Yue Xiaopeng took the Java exam. He and his father Yue buqun made a commitment:
If the score is 100, a BMW will be rewarded;
When the score is (80, 99], an apple will be rewarded;
When the score is [60,80], reward a watch;
Other times, there is no reward.
Please input Yue Xiaopeng's final grade from the keyboard and judge it

package day001;
import java.util.Scanner;
public class KaoShi {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("Please enter Yue Yunpeng's score: (0)~100)");
        int grade = scan.nextInt();
        if(grade==100){
            System.out.println("You can buy a BMW");
        }else if(grade > 80 && grade == 99){
            System.out.println("You can buy an apple");
        }else if(grade >= 60 && grade <= 80){
            System.out.println("You can buy a watch");
        }else {
            System.out.println("nothing in the world");
        }
    }
}

Example 2: input three integers into the variables num1, num2 and num3 by the keyboard, sort them (using if else), and output them from small to large.

package day001;
import java.util.Scanner;
public class Test2 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("Please enter a number num1:");
        int num1 = scan.nextInt();
        System.out.println("Please enter a number num2:");
        int num2 = scan.nextInt();
        System.out.println("Please enter a number num3:");
        int num3 = scan.nextInt();
        if(num1 >= num2){
            if(num3 >= num1){
                System.out.println(num2 + "," + num1 + "," + num3);
            }else if(num3 <= num2){
                System.out.println(num3 + "," + num2 + "," + num1);
            }else{
                System.out.println(num2 + "," + num3 + "," + num1);
            }
        }else{
            if(num3 >= num2){
                System.out.println(num1 + "," + num2 + "," + num3);
            }else if(num3 <= num1){
                System.out.println(num3 + "," + num1 + "," + num2);
            }else{
                System.out.println(num1 + "," + num3 + "," + num2);
            }
        }
    }
}

Example 3
My dog is 5 years old. How old is a 5-year-old dog equivalent to human beings? In fact, each year of the first two years of a dog is equivalent to 10.5 years of human beings, and then it increases by four years every year. So how old is a 5-year-old dog equivalent to human beings? It should be: 10.5 + 10.5 + 4 + 4 = 33 years. Write a program to obtain the age of the dog entered by the user, and the program shows that it is equivalent to human beings If the user enters a negative number, a prompt is displayed.

package day001;
import java.util.Scanner;
public class DogTest {
    public static void main(String[] args) {
     Scanner scan = new Scanner(System.in);
     System.out.print("Please enter enough age:");
     int dogAge = scan.nextInt();
     double personAge ;
     if(dogAge==1){
         personAge = 10.5;
     }else if(dogAge == 2){
         personAge = 21;

     }else{
         int i = dogAge -2;
         personAge =  21 + i * 4;
     }
        System.out.print("The dog is as old as a man:" + personAge);
    }
}

Example 4:
Programming: input "month" and "day" of 2019 from the keyboard, and the date to be input through program output is the day of 2019.

package day002;
import java.util.Scanner;
public class Test {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.print("Please enter the month of today:");
        int month = scan.nextInt();
        System.out.print("Please enter today's date:");

        int day = scan.nextInt();
        switch(month){
            case 1:
                break;
            case 2:
                day = day+29;
                break;
            case 3:
                day = day+59;
                break;
            case 4:
                day = day+90;
                break;
            case 5:
                day = day+120;
                break;
            case 6:
                day = day+151;
                break;
            case 7:
                day = day+181;
                break;
            case 8:
                day = day+212;
                break;
            case 9:
                day = day+243;
                break;
            case 10:
                day = day+273;
                break;
            case 11:
                day = day+304;
                break;
            case 12:
                day = day+334;
                break;
            default:
                System.out.println("Input error.");
   }
        System.out.println("Today is the third day of 2019" + day + "Oh, my God.");

    }
}

Example 5:
Write the program to cycle from 1 to 150, and print a value on each line. In addition, print "foo" on each multiple line of 3, "biz" on each multiple line of 5, and "baz" on each multiple line of 7.

package day002;
public class Test1 {
    public static void main(String[] args) {
        for(int i = 0;i <= 150;i++){
            System.out.print(i);
            if(i % 3 == 0){
                System.out.print("\tfoo");
            }
            if (i % 5 == 0) {
                System.out.print("\tbiz");
            }
            if (i % 7 == 0) {
                System.out.print("\tbaz");
            }
            System.out.println("");
       }
    }
}

Example 6:
Title: enter two positive integers m and n to find their maximum common divisor and minimum common multiple.
For example, the maximum common divisor of 12 and 20 is 4 and the minimum common multiple is 60.

package day002;
import java.util.Scanner;
public class mnTest {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("Please enter the first positive integer:");
        int m = scan.nextInt();
        System.out.println("Please enter the second positive integer:");
        int n = scan.nextInt();
        int min =(m <= n)?m:n;
        for(int i = min;i >= 1;i--){
             if(m % i == 0 && n % i == 0){
                 System.out.println("The maximum common divisor of two integers is:" + i);
                 break;
             }
        }
        int max =(m >= n)?m:n;
        int t = m * n;
        for(int i = max;i <= t;i++) {
            if (i % m == 0 && i % n == 0) {
                System.out.println("The least common multiple of two integers is:" + i);
                break;
            }
        }
    }
}

Example 7:
Output all the daffodils. The so-called daffodils number refers to a 3-digit number, and the sum of the number cubes on each digit is equal to itself.
For example: 153 = 111 + 333 + 555

package day002;

public class ShuiTest {
    public static void main(String[] args) {
        for(int i = 100;i < 1000;i++){
            int a = i / 100;//Hundreds
            int b = i /10 %10;//Ten digits
            int c = i % 10;//Single digit
            if(i == a*a*a + b*b*b + c*c*c){
                System.out.println(i);
            }
        }
    }
}

Example 8:
Read an uncertain number of integers from the keyboard, judge the number of positive and negative numbers, and enter 0 to end the program.

package day003;
import java.util.Scanner;
public class LiTest {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int count = 0;//Positive number
        int c = 0;//Number of negative numbers
        while(true) {
            int num = scan.nextInt();
            if (num > 0) {
                count++;
            } else if (num < 0) {
                c++;
            } else {
                break;
            }
        }
            System.out.println("Positive number" + count);
            System.out.println("Number of negative numbers" + c);
    }
}

Example 9:
Print 99 multiplication table

package day004;

public class Test {
    public static void main(String[] args) {
        for(int i = 1;i <= 9;i++){
            for(int j = 1;j <= i; j++){
                System.out.print(j + " * " + i + "==" + i * j + "    ");

            }
            System.out.println();
        }
    }
}

Example 10
Find prime numbers within 100

package day004;

public class PrimeNumberTest {
    public static void main(String[] args) {

        for(int i = 2;i <= 100;i++){
            boolean b = true;
            for(int j = 2;j <= i-1;j++){
                if(i % j == 0){
                    b = false;
                }
            }
            if(b == true){
                System.out.println(i);
            }
        }
    }
}

After optimization

package day004;
/*
Find prime numbers within 100
 */
public class PrimeNumberTest {
    public static void main(String[] args) {
       label:for(int i = 2;i <= 100;i++) {//Optimization one
                 for (int j = 2; j <= Math.sqrt(i); j++) {//Optimization II
                    if (i % j == 0) {
                    continue label;//Optimization one
                }
            }
                System.out.println(i);

       }
   }
}

Posted by simshaun on Tue, 23 Nov 2021 17:22:47 -0800