Last active
April 19, 2022 15:20
-
-
Save speters33w/fbbc276e03967400a2bec625174180b6 to your computer and use it in GitHub Desktop.
Udemy Java Masterclass Switch Statement Challenge
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
//Udemy | |
//Java Programming Masterclass covering Java 11 & Java 17 | |
//Switch Statement Challenge | |
// | |
//Create a new switch statement using char | |
//Create a new char variable | |
//Create a switch statement testing for | |
//A, B, C, D, E | |
//Display a message if any of these are found and break | |
//Add a default which displays a message saying not found | |
import java.util.Random; | |
import java.util.Scanner; | |
/** | |
* Generates a random character between A and J and looks for A, B, C, D, or E. | |
*/ | |
public class SwitchStatementChallenge { | |
/** | |
* Uses a switch statement to look for A, B, C, D, or E. | |
* | |
* @param character Character in the range [A-J] generated randomly in main(). | |
*/ | |
public static void switchStatementChallenge(char character) { | |
switch (character) { | |
case 'A': case 'B': case 'C': case 'D': case 'E': | |
System.out.println("The letter " + character + " was found."); | |
break; | |
default: | |
System.out.println("The letters A, B, C, D, or E were not found."); | |
} | |
} | |
/** | |
* Generates a random character between A-J and runs the program. | |
*/ | |
public static void main(String[] args) { | |
//get user input to generate trial cases | |
System.out.println("Press return to continue, q quits."); | |
Scanner scanner = new Scanner(System.in); | |
while (!"q".equals(scanner.nextLine()) ) { | |
//Generate a random character using decimal ASCII [65-75], [A-J] | |
Random random = new Random(); | |
int value = random.ints(65, 74) | |
.findFirst() | |
.getAsInt(); | |
char character = (char) value; | |
System.out.println(String.valueOf(character)); | |
switchStatementChallenge(character); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment