Switch case in java programming allows us to make a decision. The program given below is such a program.
After running the program it will ask you to press a number between 1 and 2. If you press 1 then you will be able to perform the mathematical operation addition. After pressing 1, you will be asked to provide two numbers to perform the selected mathematical operation.
After running the program it will ask you to press a number between 1 and 2. If you press 1 then you will be able to perform the mathematical operation addition. After pressing 1, you will be asked to provide two numbers to perform the selected mathematical operation.
Similarly if you press 2, then you will be able to perform the mathematical operation Subtraction. After pressing 2, you will be asked to provide 2 numbers for subtraction.
Thus we may perform more operations using cases. Stitch case is being used also in c programming and in web designing.
import java.util.Scanner; public class SwitchCase{ public static void main(String [] args){ //showing message for users System.out.println("Enter your Choice:\nPress 1 for Addition\nPress 2 for Subtraction"); //getting input from keyboard int choice; Scanner in = new Scanner(System.in); choice = in.nextInt(); //declaring four double type varriables for mathematical operations double num1,num2,addition,subtraction; //message to store the first number System.out.print("\n\nEnter a Number: "); //storing double type data into the variable num1 num1 = in.nextDouble(); System.out.print("Enter another Number: "); num2 = in.nextDouble(); //switch case start switch(choice){ //this section will be executed if choice=1 found case 1: addition=num1+num2; System.out.println("Addition is: "+addition); break; //this section will be executed if choice=2 found case 2: subtraction=num1-num2; System.out.println("Subtraction is: "+subtraction); break; default: break; } //ending switch case } }
Advertisement
Thanks for sharing this Switch Case program
ReplyDeleteThanks for your comment on switch case java program
Delete