Learn JavaDay04 on October 14, 2021

Posted by CostaKapo on Thu, 14 Oct 2021 21:47:35 +0200

JavaDay04

Java environment construction

JDK download and installation

Uninstall the original environment and configure the JDK

  • Delete the original environment variable, system – > environment variable – > java_ Home file location where the value is found – > delete – > then delete the environment variable
  • Delete the bin value related to Java in path
  • dos window, test whether there is a Java environment, java -version

Install JDK

  • Search JDK8, find the corresponding version, and agree to download and install the agreement
  • Installation path (remember). It is recommended that the installation directory be a non system disk
  • Configure the environment variable, system – > environment variable, add JAVA-HOME – > the value of the variable is the installation path of JDK – > configure the path variable, add the bin value, create a new% JAVA-HOME% \ bin,% JAVA-HOME% \ jre \ bin (for other versions, you need to find the jre directory)
  • Test whether the configuration environment is successful, dos window, java version, prompt the version number, and the installation is successful
  • Install notepad + + (convenient for text recording, the advantage is better than notepad recognition program and color prompt)

IDEA installation

  • IDE official website: https://www.jetbrains.com/
  • Basic open environment: a tool for eclipse to get up early and develop java
  • Enter the official website, select IntelliJ IDEA, download and install, and pay attention to the selection of path and 32 / 64 bit during installation

Control flow (condition judgment, loop statement)

Java Switch statement

  • Switch is equivalent to another expression of if else
  • Switch usage range: byte, short, int, char, string, enum
switch (day){
    case 1:
        System.out.println("Monday");
        break;
    case 2:
        System.out.println("Tuesday");
        break;
    default:
        System.out.println("other");
            
}
  • Every sentence should end with a break;
  • String is not supported before Java 1.7. After version 1.7, Switch string is supported. After compilation, string is converted to hash value
  • Use Switch to practice which season the month is
  • {} is required after switch
import java.util.Scanner;
public class Bijiao {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int month = sc.nextInt();
		switch (month){
		case 1:
		case 2:
		case 3:
			System.out.println(month+"June is spring");
			break;
		case 4:
		case 5:
		case 6:
			System.out.println(month+"June is summer");
			break;
		case 7:
		case 8:
		case 9:
			System.out.println(month+"September is autumn");
		case 10:
		case 11:
		case 12:
			System.out.println(month+"Month is winter");	//The printout can be directly "winter"
			break;
		default:	//Default default, which represents other values or other situations
			System.out.println("Please enter the correct month");
		}		
	}
}

After optimization:

import java.util.Scanner;
public class Bijiao {
	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		System.out.println("Please enter the month to identify");
		int month = s.nextInt();
		switch (month){
		case 1:
		case 2:
		case 3:
			System.out.println(month+"Month is spring");
			break;
		case 4:
		case 5:
		case 6:
			System.out.println(month+"June is summer");
			break;
		case 7:
		case 8:
		case 9:
			System.out.println(month+"Month is autumn");
			break;
		case 10:
		case 11:
		case 12:
			System.out.println(month+"Month is winter");
			break;
		default:
			System.out.println("Please enter the correct month");
			break;
		}
	}
}

While and do while loop statements

  • key wordbrief introduction
    whileRepeat when the condition is true
    do-whileIf the condition is true, the execution will be repeated at least once
public class Bijiao {
	public static void main(String[] args) {
		
		int i = 1;
		while(i<7){	//If the while expression holds, it will always loop
			System.out.println(i);
			i++;
		}
        
       //Whether do while is true or not, execute it once before making a judgment
        //Print 1 to 5
        int i = 1;
        do{
            System.out.println(i);
            i++;
        }while(i<6);
        
	}
}
  • Use Scanner to get an integer, and then use while to calculate the factorial of the integer

  • Factorial of M = M*(M-1)*(M-2)****1

    //Factorial calculation associated with an integer
    import java.util.Scanner;
    public class Bijiao {
    	public static void main(String[] args) {
    		//Calculate factorial, factorial of N = N*(N-1)*(N-2)***1
    		Scanner sc = new Scanner(System.in);//Let sc be the input value of the console
    		System.out.println("Please enter an integer to calculate factorial");
    		int n = sc.nextInt();//Calculation of int n as an integer
    		int i = 1; //i=1 is to calculate the factorial, and the calculation will not end until 1
    		while(n>0){//n> O, the integer entered in the console is > 0 to start the calculation cycle
    			i*=n;
    			n--;//After each multiplication, n is subtracted once
    		}
    		System.out.println("The factorial of is:"+i);
    	}
    }
    

The for loop is the same as While, but expressed differently

Compare for and while

public class Bijiao {
	public static void main(String[] args) {
		
		//Use the while loop to print 1-5
		int i = 1;
		while(i<6){
			System.out.println("while Cyclic output"+i);
			i++;
		}
		
		//Use the for loop to print 1-5
		for (int j = 1; j < 6; j++){//The for statement also has {},
			//Compared with while, the judgment conditions are the same, but different
			System.out.println("for Cyclic output"+j);
		}
	}
}
//There was a beggar named Hong in the Chinese dynasty. He went to the overpass to ask for money
//I asked for 1 yuan on the first day
//I asked for 2 yuan the next day
//I asked for four dollars on the third day
//Eight dollars on the fourth day
//and so on
//Question: how much does beggar Hong earn after working for 10 days?
import java.util.Scanner;
public class Bijiao {
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);//sc is the identification console input
		System.out.println("Please enter the number of days to beg:");
		int day=sc.nextInt();//day = value entered
		int salary=1;//salary is calculated from the first day
		int sum=1;//The total sum is also calculated from the first day
		if(day>0){//if judge whether the entered days are > o normal, otherwise else
			for(int i=1;i<day;i++){//for loop
				salary*=2;
				sum+=salary;
			}
			System.out.println("The first"+day+"Daily income is:"+salary);
			System.out.println(day+"The total revenue per day is:"+sum);
		}else{
			System.out.println("Input error!");
		}
	}
}
//There was a beggar named Hong in the Chinese dynasty. He went to the overpass to ask for money
//I asked for 1 yuan on the first day
//I asked for 2 yuan the next day
//I asked for four dollars on the third day
//Eight dollars on the fourth day
//and so on
//Question: how much does beggar Hong earn after working for 10 days?
import java.util.Scanner;
public class Bijiao {
	public Bijiao(int day){
		int Money = 0;//Daily income
		int MoneyAll = 0;//Total income
		for (int i = 1;i <= day;i++){//i control the cycle, and colleagues also represent days
			if (i< 2){
				Money = i;
				System.out.println("This is the first step in your work"+i+"God,"+"harvest"+Money+"Yuan.");
			}else{
				Money = Money * 2;
				System.out.println("This is the first step in your work"+i+"God,"+"harvest"+Money+"Yuan.");
			}
			MoneyAll += Money;
		}
		System.out.println("How many jobs do you have"+day+"God,"+"Total harvest"+MoneyAll+"Yuan.");
	}
	public static void main(String[] args) {
		Bijiao test = new Bijiao(10);
	}
}

Continue to continue the next cycle

Example: if it is an even number, the following code will not be executed, and the next cycle will be carried out directly

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

        //Print odd number
        for (int j = 0; j < 10;j++){
            if (0==j%2)//%2 stands for the remainder 2, whether it is an even number
                continue;//If it is an even number, the following code will not be executed and the next loop will be executed directly

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

break, end the loop

Instance: directly end the current for loop

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

        for (int j = 0; j < 10;j++){
            if (0==j%2)
                break;//If it is an even number, end the loop directly

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

Practical exercises:

Suppose your monthly income is 3000. In addition to your usual expenses, you leave 1000 yuan for investment every month.

Then you carefully studied "stocks and funds 21 days from entry to Mastery", reaching 20 every year%Return on investment.

So the problem is, with the rhythm of investing 1000 yuan a month, how many years of continuous investment will the total income reach 1 million
(Compound interest is calculated based on 12000 investment per year, not monthly interest)

Compound interest formula:
F = p* ( (1+r)^n );
F Final income,p principal,r Annual interest rate,n How many years

Scenario 1:
p = 10000
r = 0.05
n = 1

Interpretation:
The principal is 10000
 The annual interest rate is 5%
Once a year
 Compound interest income 10000*( (1+0.05)^1 ) = 10500

Scenario 2:
p = 10000
r = 0.05
n = 2

Interpretation:
The principal is 10000
 The annual interest rate is 5%
For two years
 Compound interest income 10000*( (1+0.05)^2 ) = 11025

End external loop

  • break; Only the current cycle can be ended
public class HelloWorld{
    public static void main(String[] args){
        //Print odd number    
        for (int i = 0; i < 10; i++) {
             
            for (int j = 0; j < 10; j++) {
                System.out.println(i+":"+j);
                if(0==j%2) 
                    break; //If it is an even number, end the current cycle
            }    
        }
    }
}
  • End external loop with boolean variable

    You need to modify the value of this variable in the internal loop. After each internal loop, you should judge the value of this variable in the external loop

    public class HelloWorld {
    	public static void main(String[] args) {
    		boolean breakout = false; //Flag whether to terminate the external loop
    		for (int i = 0; i < 10; i++) {
    
    			for (int j = 0; j < 10; j++) {
    				System.out.println(i + ":" + j);
    				if (0 == j % 2) {
    					breakout = true; //The flag to terminate the external loop is set to true
    					break;
    				}
    			}
    			if (breakout) //Determine whether to terminate the external loop
    				break;
    		}
    
    	}
    }
    
  • End external loop with label

    Label the line before the external loop

    Use this tag when break ing

    That is, the effect of ending the external cycle can be achieved

    public class HelloWorld{
        public static void main(String[] args){
            //Print odd number
            outloop://The logo of outlook can be customized, not all the main words
            for (int i = 0;i < 10; i++){
                for (int j = 0; j < 10; j++){
                    System.out.println(i+":"+j);
                   if(0==j%2)
                       break outloop;//If it is an even number, end the external loop
                }
            }
        }
    }
    

    Example exercise, daffodils number definition:
    \1. It must be three digits
    \2. The cube of each bit adds up to the number itself, such as 153 = 111 + 555 + 333

    Look for all the daffodils

    public class HelloWorld{
    	//The number of daffodils must be three digits. The cube of each digit adds up to itself
    	//153=1*1*1+5*5*5+3*3*3
    	//Question: find the number of all daffodils
    	public static void main(String[] args) {
    		//Analysis: 3 digits, separating hundreds, tens and individuals
    		for(int i=100;i<1000;i++){//The range of values given is 3 digits < 1000
    			int a = i%10;	//Take the number of bits
    			int b = i%100/10;	//Take ten digits
    			int c = i/100;//Take hundreds
    			if(Math.pow(a, 3)+Math.pow(b, 3)+Math.pow(c, 3)==i){
    				System.out.println(i);
    			}
    		}
    	}
    }
    

    Example exercise, find a number divided by two, and the result is closest to the golden section point of 0.618

    Denominator and numerator cannot be even at the same time
    The denominator and numerator values range from [1-20]

Topics: Java Scala Eclipse