Friday, September 11, 2015

Switch statement in java


the if single-selection statement and the ifelse double-selection
statement. The switch multiple-selection statement performs different actions based on
the possible values of a constant integral expression of type byte, short, int or char and latest version supports a string

 

 
 
you can use Strings in a switch statement’s controlling expression and

in case labels. For example, you might want to use a city’s name to obtain the corresponding

ZIP code. Assuming that city and zipCode are String variables, the following switch



statement performs this task for three cities:

the program should be like that :



import java.util.Scanner;




public class Apples {



public static void main(String[] args){

String city;

Scanner input = new Scanner(System.in);

System.out.println("enter a ctiy name that u want its zip code");

city = input.nextLine();

switch(city) // u have to enter the city with the correct spelling


{

case "Maynard":

System.out.println("zip code is 01754");

break;

case "New York":

System.out.println("zip code is 0111");

break;



case "Malroborough":

System.out.println("zip code is 01752");

break;

/* as u run it it will ask for a city name then u enter it and press enter



it should display the code */
 
}


}

}
 
the output should be something like that :
 


 

1 comment:

  1. I like the instructions with the diagrams , it makes it look easier . Thank you Mr.Gabr . Good class !!

    ReplyDelete