Java Learning (v), conditional structure

Source: Internet
Author: User

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

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.