Today is the second day of study. We have started our daily journey of typing code. There is no more nonsense. Let's start now.
Process control statement
Sequential process
An ordinary Java program, such as hello world, which we wrote yesterday, is a sequential process. It is a program executed from top to bottom. This is also the default execution process without other control process statements
Condition control
Conditional control statements include if then, if then else, if then else and if then else.
Let's explain it through the code
class demo1 { public static void main(String[] args){ int a = 3; if(a == 3){ System.out.println("a The value of is equal to 3"); } } }
The value in the if parentheses in this code is a Boolean value. If it is true, enter the following code block
The following code is if then else
class demo1 { public static void main(String[] args){ int a = 4; if(a == 3){ System.out.println("a The value of is equal to 3"); }else{ System.out.println("a The value of and 3 are not equal"); } } }
The following code will describe the if then else if then else structure when outputting Chinese
class demo2 { public static void main(String[] args){ int a = 60; if(a<=100){ if(a>=80){ System.out.println("excellent"); }else if(a>=60){ System.out.println("good"); }else{ System.out.println("fail,"); } } } }
You can write your own exercises to practice this structure of if. It's still very simple to practice more.
Cycle control
There are three loop structures: while() {}, do {} while() for( ; ; ) {} these three cycle structures are introduced separately below.
while
The following is a while code
class demo3 { public static void main(String[] args){ int i = 0; while(i<5){ System.out.println("hello world"); i++; } } }
The value in the while parentheses is a Boolean value, which can not only write comparison statements
do ... while
It is similar to while and uses the same process, but there is a difference. Here is a normal do while loop (the same as the above requirements)
class demo4 { public static void main(String[] args){ int i = 0; do{ System.out.println("hello world"); i++; }while(i<5); } }
The following code shows the difference between while and do while
class demo5 { public static void main(String[] args){ int i = 0; do{ System.out.println("hello world"); i++; }while(i<0); } }
It is obvious that i does not meet the conditions of while, but he still executes it once, indicating that do while is executed once before judging the conditions, while while while is directly judging the conditions. If the conditions are met, it will be executed again. But i don't think do while is very comfortable to use.
for
Let's put the code directly to see the use of for
class demo6 { public static void main(String[] args){ for(int i = 0;i < 5;i++){ System.out.println("hello world"); } } }
There are three parts in the for loop. The first part is the definition of the variable, the second part is to control the number of cycles of the loop, the value is of Boolean type, and the third part is self increasing or self decreasing, indicating the change of the variable. In the parentheses of the for loop, each part can be omitted, but there should be corresponding content. For example, int i = 0 in my code can be written in front of the for loop. However, if the loop control has no content or is always true, it becomes an endless loop.
Double layer cycle
Nested loops can be carried out in the loop. An example is given to explain what nested loops are. The requirement is to print the following graphics:
****
****
****
****
class demo7 { public static void main(String[] args){ for(int i = 0;i<4;i++){ for(int j = 0;j<4;j++){ //The print method does not wrap lines when outputting System.out.print("*"); } //This line means to output a whole line at a time, and then wrap it System.out.println(); } } }
Nested loops are literally nested loops in loops, also known as multiple loops. These logic can be explained by drawing or by overall thinking
1. Create a blank project and test it (not in the demonstration)
2. First use four statements to output a row of data
System.out.print("*"); System.out.print("*"); System.out.print("*"); System.out.print("*"); System.out.println();
3. It is found that the above code has duplicate code, and then you can improve it with a loop
for(int j = 0;j<4;j++){ System.out.print("*"); } System.out.println();
4. In this way, one line of code is written, and then four lines are output. The code is as follows
for(int j = 0;j<4;j++){ System.out.print("*"); } System.out.println(); for(int j = 0;j<4;j++){ System.out.print("*"); } System.out.println(); for(int j = 0;j<4;j++){ System.out.print("*"); } System.out.println(); for(int j = 0;j<4;j++){ System.out.print("*"); } System.out.println();
5. It is found that the same code is repeated again. Put the code of 3 as a whole into another loop, and let it execute the loop inside. In this way, the final code comes out
class demo7 { public static void main(String[] args){ for(int i = 0;i<4;i++){ for(int j = 0;j<4;j++){ //The print method does not wrap lines when outputting System.out.print("*"); } //This line means to output a whole line at a time, and then wrap it System.out.println(); } } }
Branch statement
There are three branch statements: break, continue and return. They all have the same function and termination function, but the results after use are not the same. Here are three statements respectively;
break
His function is to end the loop. The code is as follows:
class demo8 { public static void main(String[] args){ for(int i = 0;i<10;i++){ if(i==5){ break; } System.out.println(i); } System.out.println("It's raining in Zhengzhou"); } }
It can be seen here that break just terminates his cycle and does not affect other parts. This is break
continue
No more, just put the code
class demo9 { public static void main(String[] args){ for(int i = 0;i<10;i++){ if(i==5){ continue; } System.out.println(i); } System.out.println("It's raining in Zhengzhou"); } }
It is obvious that continue is to skip this cycle
return
Direct release code
class demo10 { public static void main(String[] args){ for(int i = 0;i<10;i++){ if(i==5){ return; } System.out.println(i); } System.out.println("It's raining in Zhengzhou"); } }
Obviously, after using return, the current method is ended directly. The current method is the main method, resulting in the output of the following statements that are raining in Zhengzhou.
Exercises
Even number found
Requirement: find all even numbers of three digits, and output the number of even numbers and their sum
analysis:
1. First create a blank item and test it
2. Demand analysis
3. Try to find all the three digits first
4. Each number is summed to 2. If the remainder is 0, it is an even number, and if it is not 0, it is an odd number
5. Define two variables, one for storing numbers and the other for storing numbers
The code is as follows
class demo011 { public static void main(String[] args){ //First define the sum of numbers and initialize int sum = 0; //Defines the number of numbers int n = 0; //Loop through all three digits for(int i = 100;i<1000;i++){ //The remainder of i is even if the remainder is 0 if(i%2 == 0){ //If it is an even number, it will enter this code block. sum+=i is another way to write sum = sum + i sum+=i; //The number of numbers also increases n++; } } //Sum of output numbers System.out.println(sum); //Number of output numbers System.out.println(n); } }
Narcissistic number
Demand: find the three digit daffodils and output them one by one. (narcissus number is the sum of the cubes of the digits of the number, which is equal to the number itself, for example: 153 = 1 ^ 3 + 5 ^ 3 + 3 ^ 3)
analysis:
1. First create a blank item and test it
2. Demand analysis
3. Find all three digits first
4. Take apart the numbers on the three digits of the number
5. Compare the cube sum of these three numbers with the original number
6. Output if the same
The code is as follows:
class demo021 { public static void main(String[] args){ //This is used to store single digit values int g = 0; //This is used to store ten digit values int s = 0; //This is used to store hundreds of digits int b = 0; //This is the cube sum used to store the above three numbers int sum = 0; //Loop through all three digits for(int i = 100;i<1000;i++){ //Propose a hundred digit value b = i / 100; //Propose a ten digit value s = (i - b * 100)/10; //Propose a bit value g = i - b * 100 - s * 10; //Calculate the cubic sum of three numbers sum = b*b*b + s*s*s + g*g*g; //Compare sum with this number if(i==sum){ //Output if same System.out.println(i); } } } }
multiplication table
Demand analysis: print out the 99 multiplication table
analysis:
1. First create a blank project and test it
2. This analysis requires nested loops
3. The outer circle is the number on the right, and the inner circle is the number on the left
4. The number on the left should be less than or equal to the number on the right
The code is as follows:
class demo041 { public static void main(String[] args){ //This is the outer loop, controlling the number on the right for(int i = 1;i<10;i++){ //This is the inner loop, the number on the left of the control for(int j = 1;j<=i;j++){ //Output a formula without line feed System.out.print(j+"*"+i+"="+j*i+" "); } //Wrap a line after one line is output System.out.println(); } } }
Find prime
Requirement: find all prime numbers within 100
analysis:
1. Create a blank project and test it
2. Traverse all numbers from 2 to 100 first
3. Analyze
4. The definition of prime number is that it can not be divided except 1 and itself, which means that the result of remainder is not 0
5. You can define a switch to represent the output of this number
6. Write a conditional statement in the loop. If the remainder result is 0, the switch becomes false
The code is as follows:
class demo031 { public static void main(String[] args){ //Traverse 2-100 numbers for(int i = 2;i<100;i++){ //Define a lock, starting with true int jishu = true; //Traverse the number of 2-i to make i remainder for(int j = 2;j<i;j++){ //If i%j is 0, it means I is not a prime number if(i%j==0){ //Lock becomes false jishu = false; } } //If jishu is true, it means that there is nothing divisible except 1 and itself, which is a prime number if(jishu){ //Output this prime number System.out.println(i); } } } }
Here is another idea of writing this topic. It is written in collections. Beginners may not understand it, but I think my idea can save a little space
The code is as follows:
import java.util.ArrayList; class demo08 { public static void main(String[] args){ ArrayList<Integer> arr = new ArrayList(); arr.add(2); for(int i = 2;i<101;i++){ boolean suo = true; for(int j = 0;j<arr.size();j++){ if(i % arr.get(j)==0){ suo = false; break; } } if(suo){ arr.add(i); } } for(int i = 0;i<arr.size();i++){ System.out.println(arr.get(i)); } } }
This uses the ArrayList set to store only prime numbers. First store a 2, and then execute the following program by using the number of 2-100 to calculate the remainder of all elements in the ArrayList. If there is anything that can be divided, the lock is false. If there is nothing that can be divided, it is prime and the lock is true. And add the new prime number to the ArrayList. In this case, only find the prime number smaller than this number, which will save a little memory. If the number is large, it may speed up a little.