This is a simple example of Switch case in Java.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | package com.xdroiddev.javapractice; import javax.sound.midi.Soundbank; import java.util.Scanner; public class Main { public static void main(String[] args) { // write your code here System.out.println("Hello Welcome to this Game!"); System.out.println("Do you want to Play this Game?"); System.out.println("Press 'Y' to Start"); System.out.println("Or Press any Key to Quit"); Scanner scan = new Scanner(System.in); String decession = scan.nextLine(); switch (decession){ case "Y": System.out.println("Let's Start the Game..."); break; default: System.out.println("Game Over"); } System.out.println("Okay, What is your Name?"); Scanner scan2 = new Scanner(System.in); String name = scan.nextLine(); System.out.println("How Are you " + name); } } |
0 Comments