Java Learning < three >

Source: Internet
Author: User

1.precedence of operators in Java:

2. Conditional statement if:

If the IF condition is set when the execution statement is only one, it is possible to omit the curly brace drop! However, if there are multiple execution statements, then curly braces are indispensable.

 Public class HelloWorld {    publicstaticvoid  main (string[] args) {        int    ;         if (one%2= =0) {            System. out. printf ("one is even ");     }}}

3.switch of the Java conditional statement:

1, switch the value of the expression after the parentheses must be integer or character type

2. The value after the case can be a constant value , such as 1, 2, or a constant expression , such as a. T, but not a variable or an expression with a variable, such as a * 2

3, case matching, execute the program code in the matching block, if you do not meet the break will continue to execute the contents of the next box block until the break statement or switch statement block end

4.whileof the Java Loop statement:

Features: first judgment, after execution

 Public classHelloWorld { Public Static voidMain (string[] args) {inti =1;//represents a number between 1-5//Loop is executed when the variable is less than or equal to 5 o'clock         while(I <=5 ) {                        //The value of the output variable, and add 1 to the variable for the next loop condition to be judgedSystem. out. println (i); I++; }    }}

5.Do...while of the Java Loop statement:

Execution process:

<1>, perform the cycle operation first, and then determine if the loop condition is true

<2>, if the conditions are established, continue to implement < 1 >, < 2, until the cycle conditions are not established

Features: first execution, after judgment

Thus, the Do...while statement guarantees that the loop is executed at least once !

 Public classHelloWorld { Public Static voidMain (string[] args) {intsum =0;//Save 1-50 even-numbered and                intnum =2;//represents an even number between 1-50                 Do {            //Implementing Cumulative Summationsum+=num; Num= num +2;//the value is added 2 for each execution once to determine the next cyclic condition                    }  while(Num <= -);//repeat loop when value is met between 1-50System. out. println ("the sum of the even numbers within 50 is:"+sum); }}

6. Forthe Java Loop statement:

Execution process:

<1>, executes the loop variable initialization section, sets the initial state of the loop, which executes only once in the entire loop

<2>, the judgment of the cyclic condition, if the condition is true, the loop body code is executed, and if False, the loop is exited directly

<3>, execute cyclic variable change part, change the value of the loop variable, in order to make the next condition judgment

<4>, re-execute < 2 >, < 3 >, < 4, until Exit loop

Features: simpler and easier to read than the while and Do...while statement structure

 Public classHelloWorld { Public Static voidMain (string[] args) {intsum =0;//Save the sum of the numbers that cannot be divisible by 3//loop variable I initial value is 1, each execution of the variable plus 1, as long as less than or equal to 100 repeats the loop         for(inti =1; I <= -; i++) {                        //The variable i and 3 are modulo (take the remainder), if not equal to 0, it means that it cannot be divisible by 3            ifI3!=0) {sum= sum + i;//Accumulate sum}} System. out. println ("The sum of the numbers between 1 and 100 that cannot be divisible by 3 are:"+sum); }}

7. Breakof the Java loop jump statement:

 Public classHelloWorld { Public Static voidMain (string[] args) {//Save Cumulative value        intsum = 0; //from 1 cycles to ten         for(inti = 1; I <= 10; i++) {                        //accumulate sum each time you loopsum = sum +i; //determines whether the accumulated value is greater than 20 and exits the loop if the condition is met            if(Sum > 20) {System.out.print ("The current cumulative value is:" +sum); //Exit Loop                 Break; }        }    }}

8.continue of the Java Loop jump statement:

 Public classHelloWorld { Public Static voidMain (string[] args) {intsum = 0;//Save Cumulative value         for(inti = 1; I <= 10; i++) {            //If I is an odd number, end the cycle and proceed to the next cycle            if(i% 2 = = 0 ) {                Continue; } Sum= Sum +i; } System.out.print ("1 to 10 of all even-numbered and for:" +sum); }}

9.multiple loops of the Java looping statement:

 Public classHelloWorld { Public Static voidMain (string[] args) {System.out.println ("Print Right triangle"); //Outer loop control number of rows         for(inti = 1; I <= 3; i++) {                        //Inner Loop controls the * numbers per line//the maximum value of the inner loop variable is equal to the value of the outer loop variable             for(intj = 1; J <= I; J + +) {System.out.print ("*"); }                        //line wrapping after each line is printedSystem.out.println (); }    }}

10. Multiple loops:

 Public classhelloworld{ Public Static voidMain (string[] args) {intnum = 999;intCount = 0;if(num >= 0 && num <= 999999999){         while(num! = 0) {Count++; Num/= 10; } System.out.println ("It's a number of" + count+ "bits! ");}Else{System.out.println ("Wrong input!" "); }   }}

11. Programming Exercises:

 Public classHelloWorld { Public Static voidMain (string[] args) {//Variable Save score        intScore = 53; //variables are saved in addition to the number of times        intCount = 0; //print output plus pre-scoreSystem.out.println ("Bonus score:%d", score); //as long as the score is less than 60, the loop performs the add-on operation and counts the number of points        if(Score < 60) {Count++; Score++; }                //print output plus score, plus number of pointsSystem.out.println ("Bonus score:%d"), score); System.out.println ("Added%d times", Count); }}

Code optimization:

12. Debugging code with Eclipse:

Java Learning < three >

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.