Swicth case with char and number

package char_switch_case;
import java.util.Scanner;
public class Char_Switch_Case {
private void number_case() {
Scanner sc = new Scanner(System.in);
System.out.println("Enter number");
int num = sc.nextInt();
switch (num) {
case 1:
System.out.println("Number 1 is called ");
break;
case 2:
System.out.println("Number 2 is called ");
break;
default:
System.out.println("small example only for num 1 and 2");
}
}
private void char_case() {
Scanner sc1 = new Scanner(System.in);
System.out.println("Enter the character ");
String input_string = sc1.nextLine();
char input_char = input_string.charAt(0);
switch (input_char) {
case 'A':
System.out.println("Char a is called ");
break;
case 'B':
System.out.println("Char b is called ");
break;
default:
System.out.println("small example only for char a and b");
}
}
public static void main(String[] args) {
Char_Switch_Case ob = new Char_Switch_Case();
ob.number_case();
ob.char_case();
System.out.println("finally i am here after switch case");
}
}

                            OUTPUT
Enter number
1
Number 1 is called
Enter the character
A
Char a is called
finally i am here after switch case

No comments:

Post a Comment