Take notes on the sixth day of learning Java EE

Keywords: Java

Learn Java EE day 6

4.7 classroom cases

4.7.1 printing isosceles triangle

public class TestNested{
	public static void main(String[] args){
		//Print isosceles triangle
		//####*
		//###***
		//##*****
		//#*******
		//*********
		int rows = 5;
		for(int i = 1;i <= rows;i++){//Outer print 5 lines
			for(int j = rows - 1;j >= i;j--){//Print 4 lines of right triangle
				System.out.print(" ");
			}
			for(int j = 1;j <= 2 * i - 1;j++){//Print isosceles triangle of 5 lines
				System.out.print("*");
			}
			System.out.println();
		}
		//Print positive right triangle (2-1 on each progressive value)
		//*
		//***
		//*****
		//*******
		//*********
		for(int i = 1;i <= rows;i++){
			for(int j = 1;j <= 2 * i - 1;j++){
				System.out.print("*");
			}
			System.out.println();
		}
	}
}

4.7.2 printing diamond

import java.util.Scanner;
public class TestNested2{
	public static void main(String[] args){
		//diamond
		//####*
		//###***
		//##*****
		//#*******
		//*********			
		//#*******
		//##*****
		//###***
		//####*
		//Diamond must be an odd number of rows
		//The console inputs a number of lines. If the number of lines is legal, the system will execute downward. If it is not, the system will ask the user to input again
		Scanner input = new Scanner(System.in);
		int rows;
		do{
			System.out.println("Please enter the number of diamond rows (odd):");
			rows = input.nextInt();
		}while(rows % 2 == 0);
		//Print diamond
		int up = rows / 2 + 1;		//Divide the diamond into two parts to print
		int down = rows / 2;
		for(int i = 1;i <= up;i++){//Print diamond top half
			for(int j = up - 1;j >= i;j--){//Print space occupation of inverted right triangle
				System.out.print(" ");
			}
			for(int j = 1;j <= 2 * i - 1;j++){//Print isosceles triangle
				System.out.print("*");
			}
			System.out.println();
		}
		for(int i = 1;i <= down;i++){//Print diamond bottom half
			for(int j = 1;j <= i;j++){//Print right triangle space occupation
				System.out.print(" ");
			}
			for(int j = 2 * down - 1;j >= 2 * i - 1;j--){//Print inverted isosceles triangle
				System.out.print("*");
			}
			System.out.println();
		}
	}
}

4.7.3 ATM service interface

import java.util.Scanner;
public class TestBankMenu{
	public static void main(String[] args){
		//ATM menu
		Scanner input = new Scanner(System.in);
		int choice;
		int flag = 0;
		do{
			System.out.println("=================Welcome to use ATM Automated banking services================");
			System.out.println("1.Open account 2.Deposit 3.Withdrawals 4.Transfer 5.Query balance 6.Change password 0.Sign out");
			System.out.println("========================================================");
			System.out.print("Please enter the operation number:");
			choice = input.nextInt();
			switch(choice){
				case 1:
					System.out.println("==Perform account opening function==");
					break;
				case 2:
					System.out.println("==Perform deposit function==");
					break;
				case 3:
					System.out.println("==Perform withdrawal function==");
					break;
				case 4:
					System.out.println("==Perform transfer function==");
					break;
				case 5:
					System.out.println("==Perform balance query function==");
					break;
				case 6:
					System.out.println("==Perform password modification function==");
					break;
				case 0:
					System.out.println("==Perform exit function==");
					break;
				default:
					flag++;
					if(flag == 3){
						break;
					}
					System.out.println("==Input error, please input again!==\n");
					break;
			}
			if(flag == 3){
				System.out.println("\n==Input error 3 times, end of program==");
				break;
			}
		}while(choice < 0 || choice > 6);
	}
}

4.8 summary

  • Concept of cycle:
    Execute a piece of logic code repeatedly through a certain condition;
  • while loop:
    while(){};
  • do while loop:
    do{}while();
  • for cycle:
    For (initial; condition; iteration) {operation;}
  • Key words of process control:
    break,continue
  • Nested loop:
    In a complete loop structure, nest another complete loop structure

5. function

5.1 definition of function

5.1.1 concept of function

  • A piece of code to realize characteristic functions, which can be used repeatedly;
  • Definition syntax:
public static void function name (){
	//Function topic
}
  • Experience:
    A group of codes that need to be reused in multiple locations are defined in the function;

5.1.2 defining functions

  • Defined location:
    The function is defined in the class and is parallel to the main function;
//Location 1
public class TestDefinitionFunction{
	//Location 2
	public static void main(String[] args){
		//Location 3
	}
	//Location 4
}
//Location 5


//Correct position: position 2, position 4

5.1.3 function call

  • Call through the function name where the function code needs to be executed;
  • Be careful:
    When the function is called, the internal code of the function will be executed first. When the function is finished, it will return to the function calling function and continue to execute downward;

5.2 parameter

5.2.1 parameters of function

  • In most cases, data interaction is needed between functions and callers;
  • The caller must provide the necessary data to make the function complete the corresponding functions;
  • When the function is called, the data passed in is called "parameter";

5.2.3 shape participation parameters

  • Definition syntax:
public static void function name (formal parameter){
	//Function body
}
  • Experience:
    "Parameter" is equivalent to "Declaration of local variable"
  • Call syntax:
Function name (actual parameter);
  • Experience:
    "Argument" is equivalent to "assignment of local variable"

5.2.4 single function

public class TestFunction2{
	public static void main(String[] args){
		System.out.println("abed, I see a silver light,");
		System.out.println("-----------");		//Normal method printing
		System.out.println("It's frost on the ground.");
		printSign(11);							//Call function print
		System.out.println("look at the bright moon,");
		printSign(12);
		System.out.println("Bow your head and think of your hometown.");
		printSign(13);
	}
	//Defining functions (print symbols), defining formal parameters
	public static void printSign(int count ){
		for(int i = 1;i <= count;i++){
			System.out.print("-");
		}
		System.out.println();
	}
}
  • Actual parameters: 10
    When calling a function with parameters, the actual parameters must be passed in to assign values to the formal parameters;
  • Formal parameter: int count
    When the function is executed, count times are cycled;

5.2.5 multiple functions

public class TestFunction2{
	public static void main(String[] args){
		System.out.println("abed, I see a silver light,");
		System.out.println("-----------");		//Normal method printing
		System.out.println("It's frost on the ground.");
		printSign(11,'#');							//Call function print
		System.out.println("look at the bright moon,");
		printSign(12,'*');
		System.out.println("Bow your head and think of your hometown.");
		printSign(13,'-');
	}
	//Defining functions (print symbols), defining formal parameters
	public static void printSign(int count , char sign){
		for(int i = 1;i <= count;i++){
			System.out.print(sign);
		}
		System.out.println();
	}
}
  • Actual parameters: 10,'x '
    When calling a function with parameters, the actual parameters, type, number and order, must correspond to the parameter;
  • Parameter: int count, char sign
    When the function is executed, print count sign times;

5.2.6 how to define parameters

  • Experience:
    The parameters of the function are defined according to the specific business requirements;
import java.util.Scanner;
public class TestFunction3{
	public static void main(String[] args){
		//Simulation of user login authentication
		//1. Enter user name and password
		//2. Verify user name (aaron) and password (123456)
		System.out.println("---Program start---");
		login();	//Call login
		System.out.println("---Program end---");
	}
	
	//Sign in
	public static void login(){		//Single function principle (one function only does one thing)
		Scanner input = new Scanner(System.in);
		System.out.print("Please enter the user name:");
		String username = input.next();
		
		System.out.print("Please input a password:");
		String password = input.next();
		
		//Call check
		check(username , password);
	}
	
	//inspect
	public static void check(String username , String password){
		//compare
		if("aaron".equals(username) && "123456".equals(password)){//String comparison is performed with ("". equals(""))
			System.out.println("Login succeeded!");
		}else{
			System.out.println("Login failed, input error");
		}
	} 
}
  1. "= =" compared address rather than content, so when comparing strings, "= =" is not accurate enough;
  2. When comparing strings, s1.equals(s2) can be used to accurately compare the contents of strings
  3. When comparing two strings that are inconsistent, apply! s1.equals(s2), (logical operator: non) represents "unequal";
Published 7 original articles, won praise 0, visited 71
Private letter follow

Posted by andreash on Mon, 10 Feb 2020 02:36:00 -0800