[英]Exception Handling a switch method
I'm trying to get a program I made to ask someone for a number between 1 and 13 in order to get them to make a selection. 我正在尝试获取一个程序,要求某人输入1到13之间的数字,以便他们进行选择。 I'm trying to figure out how to handle if they want to be contrarian and enter a non-valid number or a character or string. 我正在尝试弄清楚如果他们想相反地输入一个无效数字或字符或字符串,该如何处理。 Here's what I have so far... 到目前为止,这就是我所拥有的...
try {
attackingUnit = selectUnit(input.nextInt());
attackerUnitName = attackingUnit.getUnitName();
} catch (NullPointerException e) {
System.out.println("Invalid option, please pick a valid option\n");
showUnitSelection();
attackingUnit = selectUnit(input.nextInt());
attackerUnitName = attackingUnit.getUnitName();
}
Here's the method I'm using for a making the selection itself: 这是我用于进行选择本身的方法:
private static Unit selectUnit(int selection) {
switch (selection) {
case 1:
return Unit.GreatSwords;
case 2:
return Unit.BlackOrcs;
case 3:
return Unit.Bestigor;
case 4:
return Unit.ChaosChosen;
case 5:
return Unit.MenAtArms;
case 6:
return Unit.Executioners;
case 7:
return Unit.GraveGuard;
case 8:
return Unit.Retributors;
case 9:
return Unit.StormVermin;
case 10:
return Unit.SwordMasters;
case 11:
return Unit.TombGuard;
case 12:
return Unit.WildWoodRangers;
case 13:
return Unit.Hammerers;
}
return null;
}
I'm pretty sure I'm not doing this right, if you don't mind I'd like to hear some suggestions to consider. 如果您不介意,我很想确定我的建议不正确。
I think code will look much prettier if you do it in this way: 我认为如果您采用这种方式,代码看起来会更漂亮:
public enum Unit{
GreatSwords(1),
BlackOrcs(2),
Bestigor(3),
ChaosChosen(4),
MenAtArms(5),
Executioners(6),
GraveGuard(7),
Retributors(8),
StormVermin(9),
SwordMasters(10),
TombGuard(11),
WildWoodRangers(12),
Hammerers(13)
private int index;
public int getIndex() {
return this.index;
}
public static getUnitByIndex(int index) throws IllegalArgumentException {
return Stream.of(values())
.filter(unit -> unit.getIndex() == index)
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("Invalid value");
}
}
The idea over here would be to avoid nulls, therefore the best option would be to make a default case that return Unit.Unknown
if the switch statement does not match anything. 这里的想法是避免空值,因此最好的选择是使默认情况下,如果switch语句不匹配任何内容,则返回Unit.Unknown
。 Then you can check if the input equals Unit.Unknown
and if it does tell the user that it is invalid. 然后,您可以检查输入是否等于Unit.Unknown
以及是否确实告诉用户该输入无效。
To do this, First add in a "Unknown" member to the Unit enum. 为此,首先将一个“未知”成员添加到Unit枚举中。
Then add in a default case to your switch statement like this: 然后在默认情况下将以下内容添加到您的switch语句中:
default:
return Unit.Unknown;
Then in your original code change it to this: 然后在原始代码中将其更改为:
attackingUnit = selectUnit(input.nextInt());
attackerUnitName = attackingUnit.getUnitName();
//check if its valid
if(attackerUnitName==Unit.Unknown){
//unit is unknown ,tell the user the input was invalid
} else {
//input was valid, do what you want to do
}
Your switch code would be like with default case- 您的切换代码将与默认情况下一样-
private static Unit selectUnit(int selection) {
switch (selection) {
case 1:
return Unit.GreatSwords;
case 2:
return Unit.BlackOrcs;
case 3:
return Unit.Bestigor;
case 4:
return Unit.ChaosChosen;
case 5:
return Unit.MenAtArms;
case 6:
return Unit.Executioners;
case 7:
return Unit.GraveGuard;
case 8:
return Unit.Retributors;
case 9:
return Unit.StormVermin;
case 10:
return Unit.SwordMasters;
case 11:
return Unit.TombGuard;
case 12:
return Unit.WildWoodRangers;
case 13:
return Unit.Hammerers;
default:throw new IllegalArgumentException(""Invalid option, please pick a valid option\n");
}
}
Your handling code would be like - 您的处理代码将是-
try {
attackingUnit = selectUnit(input.nextInt());
attackerUnitName = attackingUnit.getUnitName();
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage);
showUnitSelection();
attackingUnit = selectUnit(input.nextInt());
attackerUnitName = attackingUnit.getUnitName();
}
Try this. 尝试这个。 Using exception handling for simple validation is an expensive operation, although it will work just fine. 使用异常处理进行简单验证是一项昂贵的操作,尽管它可以很好地工作。
attackingUnit = null;
while(attackingUnit == null){
System.out.println("Invalid option, please pick a valid option\n");
showUnitSelection();
attackingUnit = selectUnit(input.nextInt());
}
attackerUnitName = attackingUnit.getUnitName();
Update: Modify you method to include default case (as per @SteelToe suggestion) 更新:修改您的方法以包括默认大小写(根据@SteelToe建议)
private static Unit selectUnit(int selection) {
switch (selection) {
case 1:
return Unit.GreatSwords;
case 2:
return Unit.BlackOrcs;
case 3:
return Unit.Bestigor;
case 4:
return Unit.ChaosChosen;
case 5:
return Unit.MenAtArms;
case 6:
return Unit.Executioners;
case 7:
return Unit.GraveGuard;
case 8:
return Unit.Retributors;
case 9:
return Unit.StormVermin;
case 10:
return Unit.SwordMasters;
case 11:
return Unit.TombGuard;
case 12:
return Unit.WildWoodRangers;
case 13:
return Unit.Hammerers;
default:
return Unit.Unknown;
}
}
And do this: 并执行以下操作:
attackingUnit = Unit.Unknown;
while(attackingUnit == Unit.Unknown){
System.out.println("Invalid option, please pick a valid option\n");
showUnitSelection();
attackingUnit = selectUnit(input.nextInt());
}
attackerUnitName = attackingUnit.getUnitName();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.