Switch Statement Java basic example

Switch statement with numbers and array of strings Java example.

public class MainClass {
	public static void main(String[] arg) {
		String[] arrayOfString = { "One", "Two", "Three", "Four" };
 
		int i = 2;
		switch (i) {
			case 1: {
				System.out.println(arrayOfString[i]);
				break;
			}
			case 2: {
				System.out.println(arrayOfString[i]);
				break;
			}
			case 3: {
				System.out.println(arrayOfString[i]);
				break;
			}
			default: {
				System.out.println("Enter a valid value.");
			}
		} // END of switch
	}
}
/*
 * Three
 */

Possible:

case 1:
 System.out.println(arrayOfString[i]);
break;
 
// i love this notation 
case 1:{
 System.out.println(arrayOfString[i]);
}break;
 
case 1:{
 System.out.println(arrayOfString[i]);
 break;
}
This entry was posted in Android Examples Code and tagged , , . Bookmark the permalink.


Comments are closed.



9,827 views