Control Execution Process -- (Java study note 3), java Study Notes

Source: Internet
Author: User

Control Execution Process -- (Java study note 3), java Study Notes
The most basic form of if-else control program flow

Format:
If (boolean-expresion ){
Statement
}
Or
If (boolean-expresion ){
Statement
} Else {
Statement
}
Example:

public static int testIf(int testVal,int target){  int result = 0;  if(testVal > target){   result+=1;  }else if(testVal<target){   result -=1;  }else{   result = 0;  }  return result; }public static void main(String[] args) {  int test1 = testIf(10, 9);  System.out.println(test1);  // 1    }
Iteration statement: while, do-while, and for are used to control the loop, also known as iteration statement examples: Calculate 1 with the while, do-while, and for statements respectively! + 2! +... + 10! Factorial and.
1 while loop format: 2 while (boolean-expresion) {3 statement 4} 5 while loop factorial: 6 int i1 = 1, sum1 = 0, jc1 = 1; 7 while (i1 <= 10) {8 jc1 = jc1 * i1; 9 sum1 = sum1 + jc1; 10 i1 ++; 11} 12 System. out. println (sum1); // 4037913
1 do-while loop format: 2 do {3 statement 4} while (boolean-expresion); 5 6 do-while loop factorial: 7 int i2 = 1, sum2 = 0, jc2 = 1; 8 do {9 jc2 = jc2 * i2; 10 sum2 = sum2 + jc2; 11 i2 ++; 12} while (i2 <= 10); 13 System. out. println (sum2); // 4037913

The only difference between the while statement and the do-while statement is that the do-while statement will be executed at least once, even if the do-while loop expression is calculated as FALSE for the first time, it will be executed once, if the while loop is calculated as FALSE for the first time, its statement will not be executed.

A For for statement is a frequently used iteration format: for (initialization; boolean-expresion; step) {statemnet} initialization expression; boolean-expresion boolean expression; step
1 for Loop factorial: 2 int i3, sum3 = 0, jc3 = 1; 3 for (i3 = 1; i3 <= 10; i3 ++) {4 jc3 = i3 * jc3; 5 sum3 + = jc3; 6} 7 System. out. println (sum3); /// 4037913
Return keywords: two purposes: Specify the return value of a method, and cause the current method to exit, that is, the statement after the return statement will not be executed, and return that value. Break and continue can use break and continue to control the cycle flow in the main part of any iteration statement. break is used to forcibly exit the loop without executing the remaining statement continue in the Loop: stop executing the current iteration statement, exit from the beginning of the loop, and re-execute the next iteration.

 

Switch is divided into selection statements. According to the integer expression, the switch statement can be selected from a series of codes to execute the switch statement format: switch (key) {case value: statement break ;... Default: statement break;} key: represents an integer selection factor value: an integer switch can compare the results of the entire expression with each case value, if yes, execute the corresponding statement. If no yes, execute the default statement.
Example 1: 9x9 multiplication table 2 Method 1: nested loop (for switch) 3 public static void main (String args []) {4 // 9X9 multiplication table 5 6 int I, j; 7 // The outer control row and the inner control column 8 System. out. println ("***************** Method 1: * **************** "); 9 for (I = 1; I <= 9; I ++) {10 for (j = 1; j <= 9; j ++) {11 switch (j) {12 case :13 System. out. print (j + "x" + I + "=" + I * j); 14 break; 15 case 2: 16 if (I> = 2) {17 System. out. print ("\ t" + j + "x" + I + "=" + I * j); 18 break; 19} 20 case :21 if (I> = 3) {22 System. out. print ("\ t" + I + "x" + j + "=" + I * j); 23 break; 24} 25 case 4: 26 if (I> = 4) {27 System. out. print ("\ t" + I + "x" + j + "=" + I * j); 28 break; 29} 30 case 5:31 if (I> = 5) {32 System. out. print ("\ t" + I + "x" + j + "=" + I * j); 33 break; 34} 35 case 6:36 if (I> = 6) {37 System. out. print ("\ t" + I + "x" + j + "=" + I * j); 38 break; 39} 40 case 7:41 if (I> = 7) {42 System. out. print ("\ t" + I + "x" + j + "=" + I * j); 43 break; 44} 45 case 8:46 if (I> = 8) {47 System. out. print ("\ t" + I + "x" + j + "=" + I * j); 48 break; 49} 50 case 9:51 if (I> = 9) {52 System. out. print ("\ t" + I + "x" + j + "=" + I * j); 53 break; 54} 55} 56} 57 System. out. println (""); 58} 59 System. out. println ("***************** Method 2: * ******************* "); 60 int i1, j1; 61 for (i1 = 1; i1 <= 9; i1 ++) {62 for (j1 = 1; j1 <= i1; j1 ++) {63 System. out. print (i1 + "x" + j1 + "=" + i1 * j1 + "\ t"); 64} 65 System. out. println (""); 66} 67} 68}

 


Process Control in java

Learning JAVA will not be affected, but there will be problems with the application !~
The most difficult part to implement a program is to organize algorithms. In fact, it is very easy to build a mathematical model. If you have set up this mathematical model, it is easy to use the program to implement it. It is basically loop and judgment !~
For example, this is a classical question: a rabbit has been born every month since 3rd months after birth, and a rabbit has been born every month after the third month, if rabbits do not die, how many rabbits are there each month?
The difficulty of this question is to calculate the serial number of rabbits each month, and then find the rule, that is, write the formula, and then use the program to implement it.
When you feel confused, because you do not have enough practice, the basics of learning programming books are important, but if you only read books and do not program much, it is useless even if you memorize all the programming books !~
If you want to learn JAVA well, you are advised to find some typical program questions and practice more. Do not throw all the questions you have done. After a while, you may find that the previously written program can be optimized or you may think of other algorithms. Do not ignore the program efficiency, such as the Program for prime number calculation. If it is optimized well, it will be tens of thousands of times faster than the general algorithm.
If you just want to learn JAVA, just read the book !~
In addition, you need to think more about the learning process and think for yourself. Don't ask others just when you encounter any problems.

How to control a java program to execute an operation at a specified time

// Java. util. Timer

Import java. util. Timer;
Import java. util. TimerTask;

Public class {
Public static void main (String [] args ){
Timer timer = new Timer (false );
System. out. println ("executed every two seconds ");
Timer. schedule (new TimerTask (){

Public void run (){
System. out. println (Math. random () * 10000 );
}
},0, 2000 );
}
}
References: if you have other questions, send me a Baidu message.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.