CGBTN2111-DAY04 summary review

DAY04 review 1 for loop Format: public static void main(String[] args) { for(Start condition; Cycle condition ; Change c...
DAY04 review 1 for loop
  1. Format:
public static void main(String[] args) { for(Start condition; Cycle condition ; Change condition) { The loop body executed after the loop conditions are met } }
  1. The start condition of the loop will only be executed once at the beginning of the first round, and will not be executed later
  2. The loop structure is suitable for us to use when we need to execute something repeatedly in the program
  3. How many times a loop can be executed depends on how many values the loop variable can get, rather than the value range of the loop variable
public static void main(String[] args) { //In this case, the value range of cyclic variable i is 8 ~ 8888, but the loop is executed only 4 times //Because i only got 4 values, 8 and 888 respectively for (int i = 8; i <= 8888; i=i*10+8) { System.out.println(i); } }
  1. We can directly end all the remaining rounds of the current cycle through break
  2. We can end this cycle through continue and start the next cycle directly
  3. We can use the return keyword to end the whole method. Note that no code can be written directly after break continue return. It is unreachable code
2 nested for loop
  1. The outer loop controls the number of execution rounds, and the inner loop controls the number of execution rounds
  2. The outer loop controls the number of rows in the graph, and the inner loop controls the number of columns in this row
  3. Experience in the case:
    If the maximum value of the inner loop variable is set to a fixed value, a rectangle will be printed
//***** //***** //***** for(int i = 1; i<=3 ;i++) {//Outer loop, control the number of rounds, execute 3 rounds for(int j = 1;j<=5;j++) {//The inner loop controls the number of times per round, and is executed 5 times per round System.out.print("*"); } System.out.println();//This print statement is used to wrap lines }

If the loop variable j of the inner loop is set to change with the change of the loop variable i of the outer loop, a right triangle is printed

System.out.println("*********Print left right triangle********"); for(int i = 1;i<=6;i++) {//The outer loop controls the number of rows /**The number of * in each row of the rectangle is fixed * The maximum number of stars per row in the left right triangle changes with the number of rows, row i, star i*/ for(int j = 1; j<=i;j++) {//The inner loop controls the number of columns in this row System.out.print("*"); } System.out.println();//Print blank lines to wrap lines. Be sure to end the inner loop }

3 member variables and local variables

Member variables:
1) Location: inside class, outside method
2) Note: the member variable has its own default value of the corresponding type, so we don't need to initialize / assign it manually
3) Effective range: it takes effect in the whole class. When the class disappears, the member variables will be released
Local variables:
1) Location: in method / local code block
2) Note: assignment / initialization is required when using
3) Effective range: in the method / local code block, after the corresponding code is executed, the local variables are released

4 method
  1. Format of method definition: modifier return value type method name (parameter list)
  2. Whether a method will execute depends on whether it is called or not, and the format of the call: method name + parameter list
  3. The position of the method definition has nothing to do with it. The execution order depends on how main() is called
  4. For a method, you can set no parameters or multiple parameters. If there are parameters, you must pass the corresponding type of parameters when using the method
  5. If the return value type of a method is void, no return value is allowed
  6. If a method wants to return a value, it must set the return value type and return the result of the corresponding type
  7. When calling a method with a return value, you can receive the return value of this method. After receiving the return value, you can use it multiple times
    If you don't, you just call the function of the method and don't use the return value
package cn.tedu.method; public class TestMethod2 { public static void main(String[] args) { //We use the method name + parameter list to determine which method to call run(); eat(5); String r = play("Erlang God"); System.out.println("I am r:"+r); } //Format: modifier return value type method name (parameter list) //Requirement 1: define a method run with no parameters and no return value. Print: the DOG dog is running fast~ public static void run() { System.out.println("puppy DOG Running fast~"); } //Requirement 2: define a method eat with the parameter int n and no return value. Print: the dog will eat n meat buns tonight~ private static void eat(int n) { System.out.println("The dog is going to eat tonight"+n+"A meat bun~"); } //Requirement 3: define a method play, the parameter String host, and the return value is String "Frisbee" //Print: Dog has a good time with his master host~ public static String play(String host) { System.out.println("puppy Dog With the master"+host+"Play happily~"); return "Frisbee"; } }

Homework 1: review and consolidate all the contents of today
Assignment 2: install the IDEA tool and complete the HelloWorld case
Set keyword color annotation color font size

The first applet written by IDEA

3 December 2021, 07:15 | Views: 9303

Add new comment

For adding a comment, please log in
or create account

0 comments