Feb 12, 2012

[Java] switch, break

Syntax highlighted by http://tohtml.com/java/


// Java
// switch, break


import java.util.*;

class switch_break
{
 public static void main(String[] args)
 {
 
        System.out.println("Input number between 1 and 5");
        Scanner scan = new Scanner (System.in);

        int input=scan.nextInt();
                
        switch(input)
        {
            case 1:
                System.out.println("Whole new world");
                break;
            case 2:
                System.out.println("Two of us");
                break;
            case 3:
                System.out.println("Print with default");
                // this does not have "break", so default case will be executed.
            case 4: case 5:
                System.out.println("case 4 and 5 together");
                break;
            default:
                System.out.println("The best");
        }
 
 
 }
}



No comments:

Post a Comment