Application of Java switch

Source: Internet
Author: User

The value of the variable , the same as the value after the case, executes The statement after it, and encounters a break to Jump out of the switch structure.

if Case any value does not perform the same as the contents of a variable default after the statement ;

Break can be omitted, not recommended

Break

  • Interrupted
  • Break
  • Jump out of the loop

    If, if.. else ...

    Switch

    More suitable for interval range judgment: From ... to the ...

    More practical and equivalent comparison

    If,if...else ... must be able to understand SWITC

    Switch does not necessarily replace if,if...else ...

    ImportJava.util.Scanner; Public classdome1{ Public Static voidMain (String []args) {Scanner input=NewScanner (system.in); intDay =0; System.out.print ("Please enter your score:"); Day=Input.nextint (); if(day>100| | Day <0) {System.out.print ("Input score Error"); }        Else{ Day=day/10; Switch(day) { Case10:system.out.print ("A"); Break;  Case9:system.out.print ("B"); Break;  Case8:system.out.print ("C"); Break;  Case7:system.out.print ("D"); Break;  Case6:system.out.print ("E"); Break; default: System.out.print ("Failed"); }        }                                    }}

    ... ...

    if (day = = 1) System.out.println ("Week 1");

    else if (day = = 2) System.out.println ("Week 2");

    else if (day = = 3) System.out.println ("Week 3");

    else if (day = = 4) System.out.println ("Week 4");

    else if (day = = 5) System.out.println ("Week 5");

    else if (day = = 6) System.out.println ("Week 6");

    else if (day = = 7) System.out.println ("Sunday");

    else System.out.println ("Input Error! ”);

    ... ...

    ... ...

    Switch (day) {

    Case 1:system.out.println (" Monday "); break;

    Case 2:system.out.println (" Tuesday "); break;

    Case 3:system.out.println (" Wednesday "); break;

    Case 4:system.out.println (" Thursday "); break;

    Case 5:system.out.println (" Friday "); break;

    Case 6:system.out.println (" Saturday "); break;

    Case 7:system.out.println (" Sunday "); break;

    DEFAULT:SYSTEM.OUT.PRINTLN (" input Error! ");

    }

    ... ...

Application of Java switch

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.