Wednesday, 10 December 2014

This is a sample code to implement switch statements in java.


import java.io.*;
class SwitchExample
{
         public static void main(String args []) throws IOException
        {
                    BufferedReader br=new BufferedReader(new InputStreamReader(System.in)));

                    System.out.println("Enter 1 if you like strawberries the most");
                    System.out.println("Enter 2 if you like mangoes the most");
                    System.out.println("Enter 3 if you like  chilies the most");
                    int ch=Integer.parseInt(br.readLine());
                    switch(ch)
                    {
                             case 1: System.out.println("You have an interesting choice, I prefer mangoes over strawberries"); break;
                             case 2: System.out.println("Hey that's cool, I like 'em too !!!"); break;
                             case 3: System.out.println("Well, I guess you need some kind of a treatment buddy !!"); break;
                            default: System.out.println("You are not human, you must like at least one");
                    }
         }
}

No comments:

Post a Comment