-------Android Training, Java training, look forward to communicating with you! ----------
1. Select the structure if statement format and its use
Format of the A:IF statement:
if (comparison expression 1) {statement body 1;}else if (comparison expression 2) {Statement body 2;}else if (comparison expression 3) {statement Body 3;}... ..else {statement body n+1;}B: Execution Process:First, the comparison expression 1 is evaluated to see if the return value is True or FALSE,If true, executes the statement body 1,IF statement end. If False, then the comparison expression 2 is evaluated to see if the return value is True or FALSE,If true, executes the statement body 2,IF statement end. If False, then the comparison expression 3 is evaluated to see if the return value is True or FALSE,if all is false, the statement body n+1 is executed. C: Note: The last else can be omitted, but it is not recommended to omit, you can tell the error value outside the range eg:
1 ImportJava.util.Scanner;2 classdemo_if {3 Public Static voidMain (string[] args) {4Scanner sc =NewScanner (system.in);//keyboard input,5 while(true) {6System.out.println ("Please enter your score");//Prompt for input7 intA = Sc.nextint ();//keyboard input received with int type8 if(a>100|a<0) {//dead loop for easy testing9SYSTEM.OUT.PRINTLN ("You have entered a wrong result"));Ten}Else if(a>=90&a<=100) { OneSystem.out.println ("a")); A}Else if(a>=80&a<90) { -System.out.println ("B")); -}Else if(a>=70&a<80) { theSystem.out.println ("C et")); -}Else if(a>=60&a<70) { -System.out.println ("D et"); -}Else if(a<60) { +System.out.println ("E et")); -}Else { +SYSTEM.OUT.PRINTLN ("You have entered a wrong result")); A } at } - } -}
2. Select the structure switch statement format and its use A:Switch Format:switch (expression) {Case value 1:statement body 1;Break ;Case value 2:Statement body 2;Break ;... ..Default:statement body n+1;Break ;}Format interpretation of B:switch statements (basic data types, as long as they can be promoted to int, enumerations in reference data types (JDK1.5) and string (JDK1.7)) C: Execution processevaluates the value of an expression firstthen match the case, and execute the corresponding statement if there is one, otherwise execute the default control statementeg
1 ImportJava.util.*;2 classdome_if3{3 Public Static voidMain (string[] args) {4 //System.out.println ("Hello world!");5Scanner sc =NewScanner (system.in);//Keyboard Entry6 while(true) {//convenient test for dead cycle7System.out.println ("Please enter the number of weeks to convert");//Keyboard Entry Tips8In week =sc.nextint ();9 Switch(week) {Ten Case1: OneSystem.out.println ("Week 1"); A Break; - Case2: -System.out.println ("Week 2"); the Break; - Case3: -System.out.println ("Week 3"); - Break; + Case4: -System.out.println ("Week 4"); + Break; A Case5: atSystem.out.println ("Week 5"); - Break; - Case6: -System.out.println ("Week 6"); - Break; - Case7: inSystem.out.println ("Sunday")); - Break; to default: +SYSTEM.OUT.PRINTLN ("You entered the wrong number, please re-enter"); - } the } * } $}
3: Summarize the respective usage scenarios for the switch statement and the IF statementswitch recommends determining the fixed value whenif it is recommended to determine the interval or range* Can do with switch, if all can do, single reverse is not
-------Windows Phone 8 phone development,. NET training, look forward to communicating with you! -------
Select the difference between a struct if statement and a switch statement