JAVA learning-02-mr. Han Shunping
Detailed explanation of JAVA process control - including exercises
- Branch control
-
Single branch
/* Basic grammar if (Conditional expression){ if Statement block } */ int a = 10; if ( a > 8 ) { System.out.println("Entered if Statement block"); }
-
Double branch
/* Basic grammar if (Conditional expression){ if Statement block } else { else Statement block } */ int a = 10; if ( a > 8 ) { System.out.println("Entered if Statement block"); } else { System.out.println("Entered else Statement block"); }
-
Multi branch
/* Basic grammar if (Conditional expression){ if Statement block } else if { else if Statement block } else { else Statement block } */ int a = 10; if ( a > 8 && a <= 10) { System.out.println("Entered if Statement block"); } else if (a >= 10 && a <= 12) { System.out.println("Entered else if Statement block"); } else { System.out.println('Entered else Statement block'); }
-
Nested branch
- Include statement in branch
/* Basic grammar if (Conditional expression){ if () { } else { } } else { else Statement block } */ int a = 10; int b = 100; if ( a > 8 && a <= 10) { if( b > 77 ) { System.out.println('b Greater than 77'); } else { System.out.println('b Less than 77'); } } else { System.out.println('Entered else Statement block'); }
-
switch Statements
- Basic grammar
/*switch((expression){ case Expression constant 1: Statement 1; [ break;] // [ ] Indicates optional case Expression constant 2: Statement 2; [ break;] ...... case Expression constant n: Statement n; [ break;] [default: Statement n+1;] } */ // What day is today import java.util.Scanner; public class test_3 { public static void main(String[] args) { int sumDays = 0; Scanner scan = new Scanner(System.in); System.out.println("Please enter the year:"); int year = scan.nextInt(); System.out.println("Please enter month: (1)-12)"); int mouth = scan.nextInt(); System.out.println("Please enter day: (1)-31)"); // Enter an integer number of days int day = scan.nextInt(); switch (mouth){ case 12: sumDays += 30; case 11: sumDays += 31; case 10: sumDays += 30; case 9: sumDays += 31; case 8: sumDays += 31; case 7: sumDays += 30; case 6: sumDays += 31;//June 24 31 + 29 + 31 + 30 + 31 + 24 case 5: sumDays += 30; case 4: sumDays += 31; case 3: if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0){ sumDays += 29; }else { sumDays += 28; } //sumDays += 29; case 2: sumDays += 31; case 1: sumDays += day; System.out.println(year + "year" + mouth + "month" + day + "Day is" + year +"The second day of the year" +sumDays + "day"); } } }
- Pay attention to details
- 1. The data type of the expression should be consistent with the data type of the case, or it can be automatically converted into a comparable type
- 2. The return value type in the expression must be (byte,short,char,int,enum,String)
- 3. The value of case must be constant
- 4.default is optional
- 5. When a case is satisfied, if there is no break, the next case will be executed directly without judgment. It will not exit until there is a break
- practice
import java.util.Scanner; public class SwitchExercise{ public static void main(String[] args) { // Judging the season (using the nature of penetration) System.out.println("Please enter 1-12 month:"); Scanner myScanner = new Scanner(System.in); int month = myScanner.nextInt(); switch(month){ case 3: case 4: case 5: System.out.println(month + "June is spring"); break;//There must be a break to terminate the puncture case 6: case 7: case 8: System.out.println(month + "June is summer"); break; case 9: case 10: case 11: System.out.println(month + "September is autumn"); break; case 1: case 2: case 12: System.out.println(month + "Month is winter"); break; default: System.out.println("Please enter the correct month"); break; } } } ```
-
- Cycle control
-
for loop
- Simple cycle
public class forExercise { public static void main(String[] args) { /* Basic grammar for(Initialization statement; Circular conditional statement; Control condition statement){ Loop body statement } */ for(int i = 1; i <= 3; i++ ) { System.out.println("I love writing code"); } } }
- Nested loop
- The inner loop is the loop body statement of the outer loop
public class forExercise { public static void main(String[] args) { /* Basic grammar for(Initialization statement; Circular conditional statement; Control condition statement){ Loop body statement } */ for(int i = 1; i <= 3; i++ ) { for(int j = 1; j <= 2; j++) { System.out.println("I love writing code"); } } } }
- Exercises
import java.util.Scanner; // Import class public class MulForExercise { public static void main(String[] args) { /* 1.Statistics of two classes, each class has three students - Calculate the average score of each class and the average score of all classes - Statistics of the number of people passing the examination */ Scanner myScanner = new Scanner(System.in); int classNum = 2; int stuNum = 3; double allScore = 0; // Total score of all int passNum = 0; // Statistics of the number of people passing the examination for(int i = 1; i <= classNum; i++) { double totalScore = 0; // Count the total score of each class for(int j = 1; j <= stuNum; j++) { System.out.print("Please enter" + i + "class" + j + "Scores of students:"); int socre = myScanner.nextInt(); totalScore += socre; allScore += socre; if( socre >= 60 ) { passNum += 1; } } System.out.println(i + "The average score of the class is:" + (int)(totalScore/stuNum)); } System.out.println("The total score is:" + allScore + "The total average score is" + (allScore/(stuNum*classNum))); // 2. Print 99 multiplication table for(int i = 1; i <= 9; i++) { for(int j = 1; j <= i; j++) { System.out.print(i + "x" + j + "\t"); } System.out.println(""); // Line feed } } }
-
while Loop
- Basic grammar
/* while(Conditional expression){ Loop body statement Conditional control statement } */ int num = 1; while(num < 10) { System.out.println("num=" + num); num ++; }
- Pay attention to details
- The conditional expression must be able to return a boolean value
- while must have conditional control statements, or it will form an endless loop
-
dowhile loop
- Basic grammar
/* do { Loop body statement Conditional control statement } while(Conditional expression) */ int num = 1; do { System.out.println("num=" + num); num ++; } while(num < 10);
- matters needing attention
- The conditional expression must be able to return a boolean value
- Do while statement, the loop body must be executed once
-
braek, continue, return control statements
- break
- break is used to interrupt this cycle and directly jump the cycle without exiting the program
- It is generally used for while, for, dowhile and switch
- continue
- continue is used to skip this cycle and directly enter the next cycle without exiting the program
- It is generally used for while, for, dowhile and switch
- return
- return is used to exit the program and directly end the execution of the program. The code after the loop is no longer executed
- Similarities and differences among the three
- Neither continue nor break will exit the program, and the code after the loop can still be executed
- continue and break can specify which level of loop to jump out through the label label
public class forExercise { public static void main(String[] args) { /* Basic grammar for(Initialization statement; Circular conditional statement; Control condition statement){ Loop body statement } */ label1: for(int i = 1; i <= 3; i++ ) { label2: for(int j = 1; j <= 2; j++) { System.out.println("I love writing code"); if(j == 2) { // break; // The default is to jump out of the inner loop break label1 // Jump out of the outer loop } } } } }
- break
-
Process control operation
public class ControlProcessExercise { public static void main(String[] args) { /* If someone has 100000 yuan and passes an intersection, he needs to pay: 1.When cash > 50000 yuan, pay 5% each time 2.When cash < = 50000 yuan, pay 1000 yuan each time How many intersections can I pass? */ int money = 100000; int times = 0; while(money > 0) { if( money >50000 ) { money -= money * 0.05; times +=1; } else if (money <= 50000 && money >= 1000) { money -=1000; times +=1; } else{ break; } } System.out.println("Can pass" + times + "Secondary intersection"); // 2, Judge whether an integer is the number of water flowers System.out.print("Please enter an integer:"); int num = myScanner.nextInt(); int verification = num;//Save num to verify whether you want to int total = 0; while(num >0) { int remainder = num % 10; total += remainder*remainder*remainder; num = num / 10; } if( total == verification) { System.out.println(verification + "This number is the number of daffodils"); } else { System.out.println(verification + "This number is not daffodils"); } // 3, Output the number between 1-100 that cannot be divided by 5, one every five lines int count = 0; // Used to control line breaks for (int i =1; i<=100; i++) { if(i % 5 == 0) { continue; } else { System.out.print(i + "\t"); count ++; if(count == 5){ System.out.println(""); count = 0; } } } // 4, 1-1 / 2 + 1 / 3 + 1 / 4 Sum of 1 / 100 double total = 0; // Note the initialization definition, 1/(double)i for(double i = 1; i <= 100; i++) { if(i % 2 == 0) { total -= (1 / i); } else { total += (1 / i); } } System.out.println(total); // 0.688172179310195 // 5, Calculate 1 + (1 + 2) + (1 + 2 + 3) ++ (1+...+100) int total = 0; for (int i = 1; i <= 100; i++) { for(int j = 1; j <= i; j++) { total += j; } } System.out.println(total);// 171700 /* 6, Print a hollow isosceles triangle * * * * * * * ********* */ int starLevel = 10; // Control the number of print lines for(int i = 1; i <= starLevel; i++) { for(int k = starLevel-i; k >0; k--) { System.out.print(" "); } // Control the number of prints for(int j = 1; j <= 2*i - 1; j++) { // If it is the first or last printed star, or the last line of printed stars if(j == 1 || j == 2*i - 1 || i == starLevel) { System.out.print("*"); } else { System.out.print(" "); } } System.out.println(""); // Line feed } } } ```
-