How to use switch method in java



Hi! Today I will share with you, what I've learned for the past months ago.... about java switch method. switch statements will be using cases..inside of the main body, in every case, you can use how many numbers as much as you can, beginning from 1 to above but not limited to, the method is also indicated after the print line statement. I will write the code below so you can see how these things work 😎


import java.io.*;
public class Switch_activity{
    
    public static void main(String[] args)throws IOException{
    BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("1.Addition");
    System.out.println("2. Multiplication");
    System.out.println("3. Subtraction");
    System.out.println("4. Division");
    System.out.println("5. Percentage");
    int a = Integer.parseInt(input.readLine());
    switch(a)
    {
      
case 1: System.out.println("Addition");
      System.out.println("Enter a 1st number:");
      int b= Integer.parseInt(input.readLine());
      System.out.println("Enter a 2nd number:");
      int c= Integer.parseInt(input.readLine());
      int result = b+c;
      System.out.println("Add result:\t"+result);
      
      
case 2:System.out.println("Multiplication");
       System.out.println("Enter a 1st number:");
      int d= Integer.parseInt(input.readLine());
      System.out.println("Enter a 2nd number:");
      int e= Integer.parseInt(input.readLine());
      int result01 = d*e;
     System.out.println("Multiplication result:\t"+result01);

case 3:System.out.println("Substraction");
       System.out.println("Enter a 1st number:");
      int f= Integer.parseInt(input.readLine());
      System.out.println("Enter a 2nd number:");
      int g= Integer.parseInt(input.readLine());
      int result02 = f-g;
     System.out.println("Subtraction result:\t"+result02);

case 4:System.out.println("Division");
       System.out.println("Enter a 1st number:");
      int h= Integer.parseInt(input.readLine());
      System.out.println("Enter a 2nd number:");
      int i= Integer.parseInt(input.readLine());
      int result04 = h/i;
     System.out.println("Division result:\t"+result04);

case 5:System.out.println("Modulo/Percentage");
       System.out.println("Enter a 1st number:");
      int f= Integer.parseInt(input.readLine());
      System.out.println("Enter a 2nd number:");
      int g= Integer.parseInt(input.readLine());
      int result05 = j%k;
     System.out.println("Percentage result:\t"+result05);
    }
    
    
    }
}

Post a Comment

0 Comments