The article directories
- Multiple cycle control
-
- 1. Introduction
- 2. Analysis of multiple cycle execution steps:
- 3. Application Examples:
- 4. Classic print pyramid
Multiple cycle control
1. Introduction
- Nested loops are formed by placing one loop inside another. Where, for,while,do… While can be either an outer loop or an inner loop. [Generally, it is recommended to use two layers, no more than three layers, otherwise, the code is not readable.]
- Essentially, a nested loop is a loop body that treats the inner loop as the outer loop. If the loop condition of the inner loop is false, the inner loop can be completely broken, and the outer loop can be finished and the next loop can be started.
- Let the number of outer loops be
m
The inner layer isn
The inner loop body actually needs to executem*n
次
2. Analysis of multiple cycle execution steps:
- Please analyze the following multiple loop execution steps and write the output => memory analysis
for(int i = 0; i < 2; i++) {
for( int j = 0; j < 3; j++) {
System.out.println("i=" + i + "j="+ j); }}Copy the code
3. Application Examples:
MulForExercise01.java
(1) Count the scores of 3 classes, each class has 5 students, calculate the average score of each class and the average score of all classes [students’ scores are input from the keyboard].
- Analysis of ideas:
- Change numerous for brief
(1) First calculate the scores of 5 students in a class and score evenlyfor
1.1 createScanner
Object then receives user input
1.2 To get the average score of the class, define onedoubel sum
Accumulate the grades of the five students in the class
Scanner sc = new Scanner(System.in);
double sum = 0; // The total score of a class
for (int j = 1; j <=5; j++){ System.out.println("Please enter the first class number." + j + "A student's grade:");
double score = sc.nextDouble();
sum += score; / / cumulative
System.out.println("The results are:"+score);
}
// Because sum is the total score of 5 students
System.out.println("sum="+sum+"Average score ="+(sum /5 ));
Copy the code
(2) Count the average score of 3 classes (5 students in each class), add a for loop I to the outer layer, because each class needs to calculate the total score and average score of the students in each class, so nest a layer of for loop 3 in the outer layer, and finally change the input class to the I class
Scanner sc = new Scanner(System.in);
for (int i = 1; i <=3; i++) {// I stands for class
double sum = 0; // The total score of a class
for (int j = 1; j <= 5; j++) { //j stands for student
System.out.println("Please enter no"+i+"First in class." + j + "A student's grade:");
double score = sc.nextDouble();
sum += score; / / cumulative
System.out.println("The results are:" + score);
}
// Because sum is the total score of 5 students
System.out.println("sum=" + sum + "Average score =" + (sum / 5));
}
Copy the code
(3) Average scores of all classes
3.1 Define a variable, 'double totalScore' to accumulate the scores of all students 3.2 When the multiple loop is over, 'totalScore/(3 * 5)'Copy the code
Scanner sc = new Scanner(System.in);
double totalScore = 0;// Accumulate the total scores of all students
for (int i = 1; i <=3; i++) {// I stands for class
double sum = 0; // The total score of a class
for (int j = 1; j <= 5; j++) { //j stands for student
System.out.println("Please enter no"+i+"First in class." + j + "A student's grade:");
double score = sc.nextDouble();
sum += score; / / cumulative
System.out.println("The results are:" + score);
}
// Because sum is the total score of 5 students
System.out.println("sum=" + sum + "Average score =" + (sum / 5));
// Add sum to totalScore
totalScore += sum;
}
System.out.println("Total score of three classes"+totalScore+"Average score ="+totalScore / 15);
Copy the code
(4) Count the number of passing students in the three classes
4.1 Defining the variable int passNum = 0; If there is a student whose grade >=60, passNum++ 4.2 if >=60 passNum++Copy the code
Scanner sc = new Scanner(System.in);
double totalScore = 0;// Accumulate the total scores of all students
int passNum = 0; // Cumulative number of passes
for (int i = 1; i <=3; i++) {// I stands for class
double sum = 0; // The total score of a class
for (int j = 1; j <= 5; j++) { //j stands for student
System.out.println("Please enter no"+i+"First in class." + j + "A student's grade:");
double score = sc.nextDouble();
if (score >= 60){
passNum++;
}
sum += score; / / cumulative
System.out.println("The results are:" + score);
}
// Because sum is the total score of 5 students
System.out.println("sum=" + sum + "Average score =" + (sum / 5));
// Add sum to totalScore
totalScore += sum;
}
System.out.println("Total score of three classes"+totalScore+"Average score ="+totalScore / 15);
System.out.println("Number of passes ="+passNum);
Copy the code
(5) [efficiency, readability, structure] can be optimized, and the number of class and students can be replaced by variables respectively
Scanner sc = new Scanner(System.in);
double totalScore = 0;// Accumulate the total scores of all students
int passNum = 0; // Cumulative number of passes
int classNum = 3; // Number of classes
int stuNum = 5; // Number of students
for (int i = 1; i <= classNum; i++) {// I stands for class
double sum = 0; // The total score of a class
for (int j = 1; j <= stuNum; j++) { //j stands for student
System.out.println("Please enter no"+i+"First in class." + j + "A student's grade:");
double score = sc.nextDouble();
if (score >= 60){
passNum++;
}
sum += score; / / cumulative
System.out.println("The results are:" + score);
}
// Because sum is the total score of 5 students
System.out.println("sum=" + sum + "Average score =" + (sum / stuNum));
// Add sum to totalScore
totalScore += sum;
}
System.out.println("Total score of three classes"+totalScore+"Average score ="+totalScore / classNum*stuNum);
System.out.println("Number of passes ="+passNum);
Copy the code
(2) Count the number of passing students in three classes, each class has 5 students.
(3) Print out the multiplication table
for (int i = 1; i <= 9; i++){
for(int j = 1; j <= i; j++){
// "\t" this is the default TAB symbol of the JAVA language. Using this symbol, the printed multiplication table is nice
System.out.print("" + i + "*" + j + "=" + (i * j)+"\t");
if(i == j){
System.out.print("\n");// When the newline condition is true, the newline is printed}}Copy the code
4. Classic print pyramid
- Use the for loop to complete the following example
Please write a program that can receive an integer, representing the totalLevel, and print out the pyramid. (Stars.java) [Make it simple. Die before you live
]
- 1. Print a rectangle first
*****
*****
*****
*****
*****
Copy the code
for (int i = 1; i <= 5; i++) {
System.out.println("* * * * *");
}
Copy the code
- Print half a pyramid
* // Level 1 has 1 *支那// Layer 2 has 2 ** * *// There are 3 * * at layer 3* * * *// There are 4 * * at layer 4* * * * *// There are 5 * * at level 5
Copy the code
for (int i = 1; i <= 5; i++) { // I indicates the number of layers
// Control the number of * in each layer
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
// After each layer is printed, the line is wrapped
System.out.println("");
}
Copy the code
- Print the whole pyramid
2- * layer1
* // Layer 1 has 1 * 2 * 1 -1 has 4=(total number of layers -1) Spaces* * *// Layer 2 has 3 * 2 * 2-1 3=(total number of layers -2) Spaces* * * * *// Layer 3 has 5 * 2 * 3 -1 has 2=(total layer -3) Spaces* * * * * * *// Layer 4 has 7 * 2 * 4 -1 has 1=(total number of layers -4) Spaces* * * * * * * * *// Layer 5 has 9 * 2 * 5 -1 has 0=(total number of layers -5) Spaces
Copy the code
for (int i = 1; i <= 5; i++) { // I indicates the number of layers
// Before output *, output corresponding space = total number of layers - current layer
for (int k = 1; k <= 5-i; k++){
System.out.print("");
}
// Control the number of * in each layer
for (int j = 1; j <= 2 * i - 1; j++) {
System.out.print("*");
}
// After each layer is printed, the line is wrapped
System.out.println("");
}
Copy the code
- Print hollow pyramids [hardest]
* // The first position of the current row is *, and the last position is *
* * // The first position of the current row is *, and the last position is *
* * // Level 3 has 2 * * the first position of the current row is *, and the last position is *
* * // Level 4 has 2 * * the first position of the current row is *, and the last position is ** * * * * * * * *// Layer 5 has 9 * all outputs *
Copy the code
for (int i = 1; i <= 5; i++) { // I indicates the number of layers
// Before output *, output corresponding space = total number of layers - current layer
for (int k = 1; k <= 5-i; k++){
System.out.print("");
}
// Control the number of * in each layer
for (int j = 1; j <= 2 * i - 1; j++) {
// The first position of the current line is *, the last position is also *, the last layer all output *
if (j == 1 || j == 2 * i - 1 || i == 5){
System.out.print("*");
}else { // Output Spaces in other cases
System.out.print(""); }}// After each layer is printed, the line is wrapped
System.out.println("");
}
Copy the code
- Die before you live: 5 layers of variables
int totalLevel = 5;
- Change the 5 to 10
int totalLeavel = 10;
for (int i = 1; i <= totalLeavel; i++) { // I indicates the number of layers
// Before output *, output corresponding space = total number of layers - current layer
for (int k = 1; k <= totalLeavel-i; k++){
System.out.print("");
}
// Control the number of * in each layer
for (int j = 1; j <= 2 * i - 1; j++) {
// The first position of the current line is *, the last position is also *, the last layer all output *
if (j == 1 || j == 2 * i - 1 || i == totalLeavel){
System.out.print("*");
}else { // Output Spaces in other cases
System.out.print(""); }}// After each layer is printed, the line is wrapped
System.out.println(""); }}Copy the code