由于这是一个足够简单的场景,您可以执行以下操作:
int typeNum; // This is what holds 1, 2, 3 etc for the type of ticket
// 0 is used, so we'll just put a "NONE" as the string for it
String classType[] = {"NONE", "First Class", "Business", "Economy"};
...
System.out.println(name +
"Your class type is: " + classType[typeNum] +
"Your discount price is: " + discountPrice +
"Your finalPrice is:" + finalPrice);
进行类型到字符串映射(并且通常只使用类型)的“正确enum
”方法是使用:http ://docs.oracle.com/javase/tutorial/java/javaOO/enum.html
(更新):根据要求在没有数组的情况下执行此操作:
int typeNum; // Still the int that should be either 1, 2, or 3 for type of ticket
...
String classType;
if ( typeNum == 1 ) {
classType = "First Class";
} else if ( typeNum == 2 ) {
classType = "Business";
} else if ( typeNum == 3 ) {
classType = "Economy";
} else {
classType = "(Unrecognized Ticket Type)";
}
System.out.println(name +
"Your class type is: " + classType +
"Your discount price is: " + discountPrice +
"Your finalPrice is:" + finalPrice);