Enum Assistance I need help understanding how to use user in
Enum Assistance
I need help understanding how to use user input with enums and switch statements...
below is some test code i made up. the user types if they need help by inputing 1 for YES, 2 for NO, and anything else for MAYBE. Essentially we have to use the given enum parameters while user inputs numbers, i am confused on how to abide by these rules and have the ability to return the string in the last method. The Class this code is in a a package with a driver that gets the decision in String form by using questionHelp()
-
Sample outputs would be:
input:
Enter 1 for yes, 2 for no : 1
output:
User has indictated the following for need of assistance: yes
input:
Enter 1 for yes, 2 for no : 3
output:
User has indictated the following for need of assistance: maybe
-
-----------code-----------(sorry if the underlines are bad for copy/paste)
/////
public enum Help{YES, NO, MAYBE}
/////
public class map {
____Help assistance;
____public Stuff(Scanner newScan){
________super();
________System.out.print(\"Enter 1 for yes, 2 for no : \");
________String ???? = newScan.nextLine();
________switch(assistance){
____________case YES:
________________????;
________________break;
____________case NO:
________________????;
________________break;
____________case MAYBE:
________________????;
________________break;
________}
____public String toString(){
________return questionHelp;
____}
____public String questionHelp(){
________String decision;
________if (????){
____________decision = \"yes\";
________} else if (????) {
____________decision = \"no\";
________} else {
____________decision = \"maybe\";
________}
____return \"User has indictated the following for need of assistance: \" + decision + \".\";
____}
}
Solution
The switch statement is a construct that is used when many conditions are being tested for.
When there are many conditions, it becomes too difficult and complicated to use the if and else if constructs. Nested if/else statements arise when there are multiple alternative paths of execution based on some condition that is being tested for.
The general form of a switch statement is:
switch (variable)
{ case expression1: do something 1;
break;
case expression2: do something 2;
break;
default: do default processing;
}
In the above program the user is taking three conditional values
1 for \'Yes\'
2 for \'No\'
3 for \'Maybe\'
Through scanner function input is taken in form of 1,2 and 3.
When user select 1: User has indictated the following for need of assistance: Yes
When user select 2: User has indictated the following for need of assistance: No
When user select 3: User has indictated the following for need of assistance: Maybe


