Process Control Statements
Classic Title:
Loop from 1 to 150 and print a value on each line, print "foo" on every three multiple lines, print "biz" on every five multiple lines, and print "baz" on every seven multiple lines.
Analysis: This type of question belongs to line stitching. System.out.print() is used for line stitching and is used at the end of a line.
System.out.println() line breaks () printing star patterns are all stitching problems
Solution 1: public class Test6 { public static void main(String[] args) { /* * Analysis: Changing title belongs to character serial connection title Method 1: Divide the situation * ① num % 3 == 0 * ② num % 5 == 0 * ③ num % 7 == 0 * ④ num % 3 == 0 && num % 5 == 0 * ⑤ num % 3 == 0 && num % 7 == 0 * ⑥ num % 5 == 0 && num % 7 == 0 * ⑦ num % 3 == 0 && num % 5 == 0 && num % 7 == 0 * So you can write separately * */ //1: Get numbers from 1 to 150 //Analysis step one is because (num% 3 == 0 && num% 7 == 0 && num% 5 == 0) is more restrictive // Belongs to a subset of (num% 3 == 0) so when using the if structure the range is small to put above //Because if -- else if -- else if -- else if -- else if -- else structure encounters a code after true // It will not end directly when executed, so you should place a smaller scope on top of the executed statement for (int num = 1; num < 151; num++) { System.out.print(num); if (num % 3 == 0 && num % 7 == 0 && num % 5 == 0){ System.out.print(" foo biz baz"); }else if (num % 3 == 0 && num % 5 == 0){ System.out.print(" foo biz"); }else if (num % 3 == 0 && num % 7 == 0){ System.out.print(" foo baz"); }else if (num % 3 == 0){ System.out.print(" foo "); }else if (num % 5 == 0){ System.out.print(" biz "); }else if (num % 7 == 0){ System.out.print(" baz "); } System.out.println(); }
Solution 2: package com.java; public class Test6 { public static void main(String[] args) { //Solution 2 for (int i = 1; i < 151; i++) { System.out.print(i); if (i % 3 == 0){ System.out.print(" foo "); } if (i % 5 == 0){ System.out.print(" biz "); } if (i % 7 == 0){ System.out.print(" baz "); } System.out.println(); } } }
Method 3: Code optimization of method 2 package com.java; public class Test6 { public static void main(String[] args) { //The current code of the decimation code is redundant System.out.println(); // Code extraction If the total set of records is placed outside the loop // Place words inside the loop if they are recorded for (int i = 1; i < 151; i++) { String str = ""; if (i % 3 == 0){ str = "\tfoo "; } if (i % 5 == 0){ str += "\tbiz "; } if (i % 7 == 0 ){ str += "\tbaz "; } System.out.println(i + str); } } }
Important points of knowledge for loop testing:
(1) Initialization
(2) Circulation conditions
(3) Circulating body
(4) Iteration conditions
1>Variables declared within a loop can only use scopes within a loop
2>Initialization conditions can be brought outside the loop
3>Missing iteration condition => Dead loop infinite loop
4>Missing Loop Conditions => Dead Loop Infinite Loop
5>Missing Initialization Conditions Iteration Conditions Loop Conditions
package com.java; public class Test7 { public static void main(String[] args) { /*for (int i = 0; i < 100; ) { //Missing iteration condition System.out.println("i"); }*/ /*//Lack of cyclic condition for (int i = 0; ; i++) { System.out.println(i); }*/ //All missing for (;;) { System.out.println("hello world"); } } }
Note: When you do any problem, don't rush to write code. Code has no ideas and is worth money. First, smooth your ideas
The solution to the problem is the analysis steps of the ideas, then implemented with code.
Generate a random number formula for an interval: [m,n]
Math.random()*(n - m + 1) + n
Note that the intervals of generated random numbers are closed before and after
Small Points of Knowledge:
Application of flag identifiers: When we need to get a value at the end of the whole problem solving process, we need to use identifiers, and the details of the process are not shown.
do {
} while()
(1) Initialization
(2) Circulation conditions
(3) Circulating body
(4) Iteration conditions
Note that even if the initial conditions are not met, they will be executed once and will be used less in development.
//The num here can be arbitrarily assigned because it will be overridden if executed at least once in the do int num = -1; do { num = scanner.nextInt() }
for: When the number of loops is fixed
while: used when the number of loops is not fixed
do while must also be executed once if the initialization condition does not satisfy the loop condition
Process Control Statement Focus
break end loop can only be used within switch or loop
continue Ends This Loop Continues Next Loop Can only be used within a Loop
return End method, as long as it is used within the method
Note: Nothing else can exist under the process control statement
Popular Interpretation of Nested Cycles
Inner circulation as circulation of outer circulation
Outer control row, inner control column
Within a nested loop:
1:break Ends break layer loop
2: If there are labels, end the layer loop of labels
package com.java; public class Test7 { public static void main(String[] args) { for (int i = 1; i <= 10; i++) { for (int j = 1; j <= 10; j++) { if (j == 6) { break; } System.out.print(j + " "); } System.out.println(); } } }
package com.java; public class Test7 { public static void main(String[] args) { a: for (int i = 1; i <= 10; i++) { for (int j = 1; j <= 10; j++) { if (j == 6) { break a; } System.out.print(j + " "); } System.out.println(); } } }
continue:
Within a nested loop:
1:continue Ends Continue Layer Cycle and Continues Next Cycle
2: End this cycle if there is label continue Continue Continue Layer Cycle
package com.java; public class Test7 { public static void main(String[] args) { for (int i = 1; i <= 10; i++) { for (int j = 1; j <= 10; j++) { if (j == 6) { continue; } System.out.print(j + " "); } System.out.println(); } } }
package com.java; public class Test7 { public static void main(String[] args) { a: for (int i = 1; i <= 4; i++) { for (int j = 1; j <= 10; j++) { if (j == 6) { continue a; } System.out.print(j + " "); } System.out.println(); } } }
Classic algorithm for finding prime numbers within 100: (especially clever use of continue)
Method 1: package com.java; public class Test7 { public static void main(String[] args) { /* Find prime numbers within 1 to 100 * */ for (int i = 2; i <= 100; i++) { boolean flag = true; //for (int j = 2; j < i ; j++) { //for (int j = 2; j < i / 2; j++) { for (int j = 2; j < Math.sqrt(i); j++) { if (i % j == 0) { flag = false; break; } } if (flag) { System.out.println(i + "Is a prime number"); } } } }
Method 2: package com.java; public class Test7 { public static void main(String[] args) { /* Find prime numbers within 1 to 100 * */ a :for (int i = 2; i <= 100; i++) { // boolean flag = true; //for (int j = 2; j < i ; j++) { //for (int j = 2; j < i / 2; j++) { for (int j = 2; j < Math.sqrt(i); j++) { if (i % j == 0) { // flag = false; continue a; } } System.out.println(i + "Is a prime number"); } } }
Binary conversion:
package com.java; import java.util.Scanner; public class Test7 { public static void main(String[] args) { /* * Binary Solution * */ //keyboard Scanner scanner = new Scanner(System.in); System.out.println("input"); int num = scanner.nextInt(); int shang ; int yu; String str = ""; while (num != 0){ shang = num / 2; yu = num % 2; str = yu + str; num = shang; } System.out.println("The binary of the current number is:"+str); } }
Seek the day of the year
public class TestExer11{ public static void main(String[] args){ //1. Enter year, month and day from keyboard java.util.Scanner input = new java.util.Scanner(System.in); System.out.print("Year:"); int year = input.nextInt(); System.out.print("Month:"); int month = input.nextInt(); System.out.print("Day:"); int day = input.nextInt(); //Judges that this day is the day of the year ==>Starts on January 1, and adds up to the day of xx month xx day //(1)[1,month-1] Number of full month days //(2) the day of the month //(3) Consider whether February is 29 days separately (depending on whether year is a leap year) //2. Declare a variable days to store the total number of days, and initialize it directly as day so that you don't have to add another day int days = day; //3. Cumulative [1,month-1] Month Full Month Days switch(month){ case 12: //Accumulated January-November days += 30;//This 30 is the number of full month days in November //There's no break here, keep going case 11: //Accumulated January-October days += 31;//This 31 represents the number of full moon days in October //There's no break here, keep going case 10: days += 30;//September case 9: days += 31;//August case 8: days += 31;//July case 7: days += 30;//June case 6: days += 31;//May case 5: days += 30;//April case 4: days += 31;//March case 3: days += 28;//February //4. Consider here whether it might be 29 days if(year%4==0 && year%100!=0 || year%400==0){ days++;//One more day } case 2: days += 31;//January } //5. Output Results System.out.println(year + "year" + month + "month" + day + "Day is the year's end" + days + "day"); } }