One, if...else statements
1 //importing packages using scanner2 ImportJava.util.Scanner;3 Public classifdemo{4 Public Static voidmain (String [] args) {5Scanner input=NewScanner (system.in);6System.out.println ("Please enter holiday days");7 intHoliday=input.nextint ();//gets the integer of the keyboard input next represents the string nextdouble represents the double type8 if(holiday>6)9 {TenSystem.out.println ("Go to Hainan"); One } A Else if(holiday>3) - { -System.out.println ("Go to the seaside"); the } - Else - { -System.out.println ("Home"); + } - } +}View Code
Second, switch
1 ImportJava.util.Scanner;2 Public classswitchdemo{3 Public Static voidMain (String []args)4 {5Scanner input=NewScanner (system.in);6System.out.println ("Please enter a number between 1-5");7 intNumber=input.nextint ();8 Switch(number)9 {Ten Case1:system.out.println ("1"); Break; One Case2:system.out.println ("2"); Break; A Case3:system.out.println ("3"); Break; - Case4:system.out.println ("4"); Break; - Case5:system.out.println ("5"); Break; the default: System.out.println ("Error"); Break; - } - } -}View Code
Summary
Constants after 1.case cannot be duplicated
2.break can be omitted, once omitted, the program will be executed down until the end of the break or switch is met
3.case order can be reversed, default can be placed in any position, usually put in the end, you can omit
4.SWITC is used to match constants, types with Byte,short,int,char,string (jdk1.7 and above), Enum (jdk1.5)
5.switch and multiple If...else statement comparisons
switch is suitable for the equivalent judgment, not suitable for interval judgment, the grammar is more concise and intuitive when making equivalent judgments.
Multi-If statement functionality is more comprehensive than switch
Java Learning (v), conditional structure