"有更好的方法吗?我觉得我不喜欢这样做。理想情况下,我只是向用户提问,让他们输入答案,处理异常,确认原始答案,然后处理这些潜在的异常。我基本上只是想问一个问题并处理任何问题,然后确认选择。
**我知道我没有看到某些物体。这更像是一个通用的代码布局问题,而不是"为什么我的代码不起作用,因为这确实有效,只是觉得我打了很多东西。**
int y = 0;
do {
int x = 0;
do {
try {
System.out.println("Please select which class you would like to be!");
System.out.println("1. Magician");
System.out.println("2. Archer");
System.out.println("3. Warrior");
int classSelection = sc.nextInt();
switch (classSelection) {
case 1:
Hero.magicianClass();
System.out.println("You have selected the Magician class\n");
x = 1;
break;
case 2:
Hero.archerClass();
System.out.println("You have selected the Archer class\n");
x = 1;
break;
case 3:
Hero.warriorClass();
System.out.println("You have selected the Warrior class\n");
x = 1;
break;
default:
System.out.println("You have incorrectly selected your class. Please try again!\n");
x = 0;
break;
}
} catch (Exception e) {
System.out.println("You did no insert an integer. Please try again!\n");
sc.next();
}
} while (x == 0);
try {
Hero.displayClass();
System.out.println("Are you certain that you want to be a " + Hero.checkClassName + "?");
System.out.println("\npress 0 for no\npress 1 for yes");
int selection = sc.nextInt();
switch (selection) {
case 1:
y = 0;
x = 0;
case 2:
y = 1;
}
} catch (Exception e) {
System.out.println("You did not insert an integer. Please try again!\n");
sc.next();
}
} while (y == 0);