I'm writing a polynomial calculator for a course assignment and I want to use enum in the switch case.
this is the enum method I wrote:
enum Options{
ADDITION(1),
MULTIPLICATION(2),
EVALUATION(3),
DERIVIATE(4),
EXIT(5);
public final int value;
Options(int value){
this.value = value;
}
public int getValue() {
return value;
}
public static Options convert(int n) {
for(Options o : values())
if(o.getValue() == n)
return o;
return null;
}
}
this switch case in the program:
choice = mysc.nextInt();
Options o = Options.convert(choice);
switch(o)
{
case ADDITION: Add(isRational);break;
case MULTIPLICATION: Mul(isRational);break;
case EVALUATION: Evaluate(isRational);break;
case DERIVIATE: Derivative(isRational);break;
case EXIT: break;
}
I want to use enum in the switch case but I want the input from the user to be an int, because it's a requirement for the assignment.
I want to convert the int I get from the user to the corresponding enum and use it in the switch case.
is there a way to convert it to the correct enum without using the convert method inside the enum?