Java Program: How to Use Switch Statement in Java

Java Program: How to Use Switch Statement in Java
NEW 4″ Multi-Touch Android 4.0 Smart Phone Dual SIM WIFI Unlocked AT&T T-Mobile
Current Bid: $63.96

How to program in Java using Switch Statement with Free Java Source Code given below:

Switch statement in java is another branching mechanism that uses cases. Basically Switch statement requires one variable to represent the choice of the user. So, we as a programmer would provide choices in the program and compare the choice of the user to our predefined choices we write inside the program. For better understanding, we will reprogram again the bird identifier written in the multi if else statement and make it a switch statement instead. Believe me, the code is much cleaner here on the switch statement than on the multi if else statement. See the programming syntax below.

Programming Syntax for Switch statement

switch(ans)
{
Case 1:
{
Your code goes here
}
Case 2:
{
Your code goes here
}
Default:
{
Your code goes here
}
}

Free Java Source Code for Switch Statement

package birds_identifier2;
import java.util.Scanner;
public class Main {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println(“–Select a Choice Below–“);
System.out.println(“Press [1]: If your Pet is can fly but cannot talk.”);
System.out.println(“Press [2]: If your Pet is cannot fly but can talk.”);
System.out.println(“Press [3]: If your Pet is cannot fly but can swim.”);
System.out.println(“Press [4]: If your Pet is cannot fly and cannot swim .”);
System.out.println();
System.out.print(“Enter your choice: “);
int user_answer = input.nextInt();
switch(user_answer)
{
case 1:
{
System.out.println(“Your Pet is an EAGLE!”);
break;
}
case 2:
{
System.out.println(“Your Pet is a PARROT!”);
break;
}
case 3:
{
System.out.println(“Your Pet is a DUCK!”);
break;
}
case 4:
{
System.out.println(“Your Pet is a CHICKEN!”);
break;
}
default:
System.out.println(“Program Error…:D”);
}
}
}

Sample Output:

See all 5 photos
See all 5 photos
See all 5 photos
See all 5 photos
See all 5 photos

Related Java Tutorials:

5 Important Tips to Learn Java Programming and Other Programming Languages
Basic Knowledge Required in Programming
How to Program in Java Using Netbeans: Complete Simple Easy Steps
Java Tutorial Examples
Java Tutorial for Beginners: A Beginners Guide on Learning Java Programming
Java Simple Codes for Beginners

Java Books Beginners

Programming with Java : A Multimedia Approach by Radhika S. Grover (2011,…
Current Bid: $.99
Introduction to Java Programming, Comprehensive Version 9e by Y. Daniel Liang
Current Bid: $50.80

Java Video Tutorial

3.2″ Touch Screen 32G Quad Band Java Mobile Cell Phone Bluetooth FM, Pic/Video
Current Bid: $49.99

Other Java Source Code Samples:

Java source code Sample: Print Different Asterisk Shapes in Recursion
Java Source code sample: How to Add Numbers inside an Array using For Loop
Java Source Code: Binary Search in Recursion
Java source code in Recursive function for X to the power Y
Java Source Code Determine Person’s Salutation and Current Age
Java Code Sample: Sort Numbers in Bubble Sort
Java Source Code: A Recursive Asterisk Diamond Shape Program
Java Source Code in a Recursive Linear Search
Java Source code: How to Add numbers inside an Array Using Recursion
Java Source code: Reverse String in Java Using Recursion
Java Source code on Printing the Greatest Common Divisor (GCD) using Recursion
Java Source Code Recursion: Recursive Koch Snow Flakes
Java Source Code: Sort Numbers in Selection Sort
Class in Java: Learn More about Java Classes with Sample Java Codes
Java Program: How to Use If Statement in Java Programming
Java Program: How to Parse a String into Integer in Java Programming
Java Program: Count the Number of String Characters in Java
Java Program: Reverse String in Java Using For Loop
Java Program: Using Multi If and Else Statement in Java Programming
Java Program: Palindrome Test Java Source Code