Java Learning Lesson 03

Source: Internet
Author: User

Java three major Process control

* Sequential control: The code executes sequentially from top to bottom;

* Condition control;

* Cycle control.

Conditional control: The condition has a true or false characteristic, and can only be true or false.

* If statement

Import java.util.scanner;public class scoredemo {public static void main ( String[] args) {System.out.println ("Please enter your score:"); Scanner sc=new scanner (system.in); Int score=sc.nextint ();          //  comparison expressions whether simple or complex, the result must be a Boolean type  if (score>=90)       &NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN ("Excellent! ");          //if statement control Statement Body if it is a statement, the curly braces can be omitted;     if (score>=80&&score<90) {         system.out.println ("Good! ");       }     if (score>=60&&score<80) { &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN ("Pass! ");       }     if (score>=0&&score<60) {          system.out. println ("Less than a lattice!" ");       }}}

650) this.width=650; "Src=" https://s2.51cto.com/wyfs02/M00/07/DA/wKiom1nSW8nz6CMPAAAY0JSt0jw737.png-wh_500x0-wm_ 3-wmp_4-s_1991320260.png "title=" W}bkioi}4d8k_koibs7z$kl.png "alt=" wkiom1nsw8nz6cmpaaay0jst0jw737.png-wh_50 "/ >


* If......else If......else if (can be connected to n else if) ... else statement

The last else can be omitted, but it is recommended to retain else to ensure the integrity of the program and improve readability.



  import java.util.scanner;//Guide package, as long as the class is not Java.long package to guide the package;  public class  Agedemo{  public static void main (String[] args) {         //Keyboard Entry       system.out.println ("Please enter your Age:");       scanner sc=new scanner (system.in);       int age =sc.nextint ();       //enter the age into the loop to match the condition       if >60) {            system.out.println ("Old man!!! ");          }             else if (age>=40&&age<60) {             system.out.println ("Middle-aged!! ");           }  &Nbsp;    else if (age>=18&&age<40) {       &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN ("Boy!! ");           }       else {             system.out.println ("Underage!! ");           }       }}


650) this.width=650; "Src=" https://s2.51cto.com/wyfs02/M00/A6/82/wKioL1nQ9liAj3B6AAAWHWjfp9E015.png-wh_500x0-wm_ 3-wmp_4-s_1367343336.png "title=" W}bkioi}4d8k_koibs7z$kl.png "alt=" wkiol1nq9liaj3b6aaawhwjfp9e015.png-wh_50 "/ >

* Switch (judging value) {

Case value: Code of Implementation;

Break ;

Case value: Code of Implementation;

Break

Case value: Code of Implementation;

Break

Default: the Code of Implementation;

Break

}

the () after switch () can not be omitted;

A shape or character type followed by a switch;

Case value after plus: can only be literal constants (either, ' a ') or constant expressions (2*3)

Break out of a matching condition; break if the ellipsis program continues until it encounters a break, it jumps out of the loop.

Default: What is executed when no conditions are matched

public class switchdemo{    public  Static void main (String[] args) {    int food=5;     switch (food) {    case 1:    case 2:     case 3:system.out.println ("Eat dumplings today!!");     break;    case 4:system.out.println ("Eat rice today!"); &NBSP;&NBSP;&NBSP;&NBSP;BREAK;&NBSP;&NBSP;&NBSP;&NBSP;CASE&NBSP;5:SYSTEM.OUT.PRINTLN ("No food today!!!");     break;    case 6:system.out.println ("There's a treat today!");     break;    case 7:system.out.println ("Eat noodles today!!");     break;        }  }} 

650) this.width=650; "Src=" https://s3.51cto.com/wyfs02/M00/07/DF/wKiom1nTe9GhaS8UAAAV-lEz0L4329.png-wh_500x0-wm_ 3-wmp_4-s_2998364164.png "title=") uny$nttwt_smgd) ygc]cml.png "alt=" wkiom1nte9ghas8uaaav-lez0l4329.png-wh_50 "/ >

* Type of expression:


int a = 5;

A*3 (Result 15)

The output type of the expression evaluates to the type of the result of the expression


Double num = 1.5;

NUM * 4 (6.0, the type that the expression is ultimately treated as is the double type)

Loop control

* While loop


Structure:

while (loop condition) {

Loop-Implemented code

}

* Ask 1-50 (both sides are closed intervals), all even-numbered and.

Public class evennumdemo{    public static void main (String[]  args) {        int arr[]=new int[50];         int sum=0;        int i=0;           while (i<arr.length) {            arr[i]= (i+1);            if ((arr[i]%2)!=0) {                  sum+=arr[i];           }            i++;            }        system.out.println (the sum of even numbers of "1-50" is: "+sum) ;}}

650) this.width=650; "Src=" https://s4.51cto.com/wyfs02/M01/08/26/wKiom1nc78eyJAOfAAAZlfV_5mI111.png-wh_500x0-wm_ 3-wmp_4-s_2899204585.png "title=" (8x9alx3fuu23ifbz35r$sc.png "alt=" wkiom1nc78eyjaofaaazlfv_5mi111.png-wh_50 "/ >


* do. While loop

Structure:


do{

Code of Implementation

}while (cyclic conditions);


* For Loop

The highest utilization rate;


Structure:


for (loop variable initialization; cyclic condition judgment; change of cyclic variable) {

The logic that loops need to implement

}

* Ask 1-100 natural number, where all the even and, all odd and, and seek all even and with all odd and difference.

Public class numsumdemo{    public static void main (String[]  args) {        int max_num=100;         int evenSum=0;        int oddSum=0;         int poor=0;         int j=0;        for (int i=0;i<max_num;i++) {             j=i+1;             if ((j%2)!=0) {                  oddsum+= (i+1);  //2 Not equal to 0: odd and             }else{                  evensum+= (i+1);  //even and            }          }         poor= ( Evensum-oddsum);          system.out.println ("1-100 even numbers:" +evenSum) ;          system.out.println ("1-100 Odd and for:" +oddsum);          system.out.println ("1-100 even and with odd and difference:" +poor);}}

650) this.width=650; "Src=" https://s3.51cto.com/wyfs02/M01/08/27/wKiom1nc9dDB1lG_AAAffmcY1xE925.png-wh_500x0-wm_ 3-wmp_4-s_3067600412.png "title=" _xj ' 5J (m__]cad[b4g]sfr2.png "alt=" wkiom1nc9ddb1lg_aaaffmcy1xe925.png-wh_50 "/ >

* Foreach Loop (not a regular Java-provided way of looping, he's just a Java-provided syntax for sugar)

foreach: A way to name a loop, a simplification of the general loop.

* Syntax sugar: For basic syntax, a simplified implementation is provided.


* Two keywords: applied to the loop control, the meaning of continue is to jump out of the cycle, break is the meaning of jumping out of the entire loop.

Nesting of loops:

public class stardemo{    public  Static void main (String[] args) {        int max_ Num=4;        for (int i=max_num;i>=0;i--) {             for (int j=0;j<i;j++) {                 system.out.print ("*");  //output, no line break             }             system.out.println ("*");  //output, default line wrapping          }}} 

650) this.width=650; "Src=" https://s2.51cto.com/wyfs02/M02/A6/DB/wKioL1ncmmnB843gAAARNeKxa6I490.png-wh_500x0-wm_ 3-wmp_4-s_2096161381.png "title=" 0{fp{%pq7 ' 8{ie6 ' @R5NGVT. png "alt=" wkiol1ncmmnb843gaaarnekxa6i490.png-wh_50 "/ >




Java Learning Lesson 03

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.