-
-
Save thinkexe/6999296fe4339baa748061c5d7d4ae16 to your computer and use it in GitHub Desktop.
Using Switch Statements
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.company; | |
import com.sun.jdi.Value; | |
import org.w3c.dom.ls.LSOutput; | |
public class Main { | |
public static <value> void main(String[] args) { | |
outputResult(1, 'A'); | |
} | |
public static void ifElse(int value){ | |
if (value == 1) { | |
System.out.println("ifElse value was 1"); | |
} else if (value == 2) { | |
System.out.println("ifElse value was 2"); | |
} else { | |
System.out.println("ifElse value was not 1 or 2."); | |
} | |
} | |
public static int switchNumber(int value){ | |
switch(value){ | |
case 1: | |
System.out.println("Switch value was 1"); | |
break; // break stops the process after a case is identified | |
case 2: | |
System.out.println("Switch value was 2"); | |
break; | |
case 3: case 4: case 5: | |
System.out.println("The switch value was: " + value); | |
break; | |
} return -1; | |
} | |
public static void switchChar(char letter){ | |
switch(letter){ | |
case 'A': | |
System.out.println("Your letter was 'A'"); | |
break; | |
case 'B': | |
System.out.println("Your letter was 'B'"); | |
break; | |
case 'C': | |
System.out.println("Your letter was 'C'"); | |
break; | |
case'D': | |
System .out.println("Your letter was 'D'"); | |
break; | |
case'E': | |
System.out.println("Your letter was 'E'"); | |
break; | |
default: | |
System.out.println("Your letter was not an option."); | |
} | |
} | |
public static void outputResult(int value, char letter){ | |
ifElse(value); | |
System.out.println(switchNumber(value) == -1 ? "Switch value was out of range." + | |
" Value = " + value:switchNumber(value)); | |
switchChar(letter); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment