Multiple Loops and Program Debugging
Objectives:
1: Master Java double loops (2-tier nested loops)
2: Use jump statements to control program flow
- break
- continue
3: Debugging
Java Dual Loop
Requirement: Print some graphics
Standard method: Outer loop controls how many lines are printed, and inner loop controls the contents of each line.
Train of thought:
1: Look at what each line of the graph is doing first. Can you work it out in a circle?
2: After each line of objects can be completed, see if it can be done n lines.
Figure 1:
/* Print the following graphics ***** ***** ***** ***** */ public class Demo1 { public static void main(String[] args){ // Output row number for(int i = 1 ; i<= 4;i++){ // Output the contents of each line for(int j = 1;j<= 5;j++) { System.out.print("*"); } // Line feed System.out.println(); } } }
Figure two:
/* Print the following graphics * *** ***** ******* ********* */ public class Demo2 { public static void main(String[] args) { // Output row number for(int i = 1;i<=5;i++) { // Output the contents of each line for(int j = 1;j<= (2*i-1);j++) { System.out.print("*"); } System.out.println(); } } }
Figure three
/* Print the following graphics * *** ***** ******* */ public class Demo3 { public static void main(String[] args) { // Output row number for(int i = 1;i<= 4;i++) { // Output the contents of each line // Output space for(int k =4;k>i;k--) { System.out.print(" "); } // Output * for(int j=1;j<=2*i-1;j++) { System.out.print("*"); } // Line feed System.out.println(); } } }
Print 9x9 multiplication table
// Positive Order of Multiplication Formula Table public class Demo4 { public static void main(String[] args) { // Output row number for(int i = 1; i <= 9;i++) { // Output elements for each line for(int j = 1;j<=i;j++) { System.out.print(j+"*"+i+"="+i*j+" "); } // Line feed System.out.println(); } } }
The grammar of multiple loops:
Multiple loops refer to nested loops, but generally nested loops have two layers, so multiple loops are called double loops.
You can think of circular structures that can be nested at will.
Multiple loops: Outermost loops are called outer loops, and nested loops are called inner loops.
The outer loop is executed once, and the inner loop is executed once.
while(){ while(){ } } while(){ do{ }while(); } for(){ while(){ } do{ }while(); }
Jump statement control program flow
Dead Cycle: An Unending Cycle
while(true){ } do{ }while(ture); for(;;){ }
Break (interruption)
- Represents the end of the loop in a loop (multiple loops only end the current loop)
- Express termination judgment in switch
//Needs: Enter the scores of 5 students and calculate the number of students with more than 80 points. //Import Scanner import java.util.Scanner; public class Demo5 { public static void main(String[] args) { // create object Scanner input = new Scanner(System.in); // Create Number of Variables int num = 0; // Input scores one by one for(int i = 1; i <= 5; i++) { System.out.print("Please input number 1."+i+"The results of the famous students are as follows:"); double score = input.nextDouble(); // Create cycle if(score < 0 || score > 100) { System.out.println("Input error!"); // End the current cycle break; }else if(score > 80) { num ++; } } System.out.println("The number of students with more than 80 points is as follows:"+num); } }
Continue (continue)
equals: Compare the contents of strings to be the same
// Enter the username and password, log in successfully if correct, and retry if unsuccessful. // Import Scanner import java.util.Scanner; public class Demo6{ public static void main(String[] args) { // create object Scanner input = new Scanner(System.in); System.out.print("Please enter your username:"); // enter one user name String username = input.next(); do { System.out.print("Please enter your password:"); // Input password String password = input.next(); // Set the password to determine whether the password is correct if(!"messi10".equals(password)) { System.out.println("Error password! Please try again!"); // End this cycle and continue the next cycle continue; } //Loop termination break; }while(true); // Successful login prompted after correct input System.out.println("Log in successfully!"); } }
continue can only appear in a loop! It means ending this cycle and entering the next one.
Debugging is super important, you must know!!!
There is no perfect program in the world. Do you try your best to make our program near perfect?
bug: bedbug
Solutions to the problem:
1: Read the code and visualize the results of the operation
2: Print content by output statement (System.out.println()/ log
3: Through the debug tool
breakpoint: The location of interrupts in a program (where pauses occur)
Variables: Local variable s displayed in the current method (often observed in debug)
(1) Double-click on the left side to predict possible problems
(2) Running the program in debug as mode, he will automatically run to the breakpoint position and then pause, waiting for your next instruction, and the selected line is currently in the state of being executed but not yet executed.
-
F5 (step into) (currently understood) means entering the method to be executed
-
F6 (step over) (must be mastered) means to execute the code on the current line and then switch to the next line.
-
F8 (resume) (must be mastered) Quickly release to the next breakpoint (skip the part we don't want to see)
ariable: Shows local variables in the current method (often observed in debug)
(1) Double-click on the left side to predict possible problems
(2) Running the program in debug as mode, he will automatically run to the breakpoint position and then pause, waiting for your next instruction, and the selected line is currently in the state of being executed but not yet executed.
-
F5 (step into) (currently understood) means entering the method to be executed
-
F6 (step over) (must be mastered) means to execute the code on the current line and then switch to the next line.
-
F8 (resume) (must be mastered) Quickly release to the next breakpoint (skip the part we don't want to see)
(3) Observing information such as variable tables, finding problems and solving problems