Take notes on the sixth day of learning Java EE
Keywords:
Java
4.7 classroom cases
4.7.1 printing isosceles triangle
public class TestNested{
public static void main(String[] args){
int rows = 5;
for(int i = 1;i <= rows;i++){
for(int j = rows - 1;j >= i;j--){
System.out.print(" ");
}
for(int j = 1;j <= 2 * i - 1;j++){
System.out.print("*");
}
System.out.println();
}
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){
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);
int up = rows / 2 + 1;
int down = rows / 2;
for(int i = 1;i <= up;i++){
for(int j = up - 1;j >= i;j--){
System.out.print(" ");
}
for(int j = 1;j <= 2 * i - 1;j++){
System.out.print("*");
}
System.out.println();
}
for(int i = 1;i <= down;i++){
for(int j = 1;j <= i;j++){
System.out.print(" ");
}
for(int j = 2 * down - 1;j >= 2 * i - 1;j--){
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){
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;
public class TestDefinitionFunction{
public static void main(String[] args){
}
}
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
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("-----------");
System.out.println("It's frost on the ground.");
printSign(11);
System.out.println("look at the bright moon,");
printSign(12);
System.out.println("Bow your head and think of your hometown.");
printSign(13);
}
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("-----------");
System.out.println("It's frost on the ground.");
printSign(11,'#');
System.out.println("look at the bright moon,");
printSign(12,'*');
System.out.println("Bow your head and think of your hometown.");
printSign(13,'-');
}
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){
System.out.println("---Program start---");
login();
System.out.println("---Program end---");
}
public static void login(){
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();
check(username , password);
}
public static void check(String username , String password){
if("aaron".equals(username) && "123456".equals(password)){
System.out.println("Login succeeded!");
}else{
System.out.println("Login failed, input error");
}
}
}
- "= =" compared address rather than content, so when comparing strings, "= =" is not accurate enough;
- When comparing strings, s1.equals(s2) can be used to accurately compare the contents of strings
- 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
Posted by andreash on Mon, 10 Feb 2020 02:36:00 -0800