我想在输入字符或错误字符时不断抛出错误消息,我的循环适用于1-5以外的数字。
我的班级premierLeagueClubs
只显示文字。我猜我需要try catch
块,可能是InputMismatchException
,但我不确定代码中的哪个地方可以放这个?
import java.util.*;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Mainmenu3 extends premierLeagueClubs{
public static void main(String args[]){
boolean valid = false;
int option = 0;
while (!valid) {
Scanner in = new Scanner(System.in);
menu();
System.out.println("\n");
option = in.nextInt();
valid = option > 0 && option < 6;
switch (option) {
case 1:
chooseTeam();
case 2:
createProfile();
//break;
case 3:
loadSave();
// break;
case 4:
credits();
// break;
case 5:
System.out.println("Goodbye!");
}
} // end switch
} // end main method
public static void chooseTeam(){
System.out.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
System.out.println("Select Team : ");
System.out.println("1. Arsenal");
System.out.println("2. Aston Villa");
System.out.println("3. Bournemouth");
System.out.println("4. Chelsea");
System.out.println("5. Crystal Palace");
System.out.println("6. Everton");
System.out.println("7. Leicester City");
System.out.println("8. Liverpool");
System.out.println("9. Manchester United");
System.out.println("10. Manchester City");
System.out.println("11. Newcastle United");
System.out.println("12. Norwich City");
System.out.println("13. Southampton");
System.out.println("14. Stoke City");
System.out.println("15. Sunderland");
System.out.println("16. Swansea City");
System.out.println("17. Tottenham Hotspur");
System.out.println("18. Watford");
System.out.println("19. West Brom");
System.out.println("20. West Ham United");
int option = 0;
Scanner in = new Scanner(System.in);
//menu();
System.out.println("\n");
option = in.nextInt();
System.out.println("You entered : " + option);
if (option == 1)
arsenal();
else
if (option == 2)
astonVilla();
else
if (option == 3)
bournemouth();
else
if (option == 4)
chelsea();
else
if (option == 5)
crystalPalace();
else
if (option == 6)
everton();
else
if (option == 7)
leicester();
else
if (option == 8)
liverpool();
else
if (option == 9)
manchesterUnited();
else
if (option == 10)
manchesterCity();
else
if (option == 11)
newcastleUnited();
else
if (option == 12)
norwichCity();
else
if (option == 13)
southampton();
else
if (option == 14)
stokeCity();
else
if (option == 15)
sunderland();
else
if (option == 16)
swanseaCity();
else
if (option == 17)
tottenhamHotspur();
else
if (option == 18)
watford();
else
if (option == 19)
westBrom();
else
if (option == 20)
westHamUnited();
} // end chooseTeam
public static void createProfile(){
} // end createProfile
public static void loadSave(){
} // end loadSave
public static void credits(){
} // end credits
public static void menu(){
System.out.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
System.out.println("Created by Darren Estcourt");
System.out.println("\n");
System.out.println("Please choose an option : ");
System.out.println("\n");
System.out.println("1. Choose team");
System.out.println("\n");
System.out.println("2. Create profile");
System.out.println("\n");
System.out.println("3. Load/Save game");
System.out.println("\n");
System.out.println("4. Credits");
System.out.println("\n");
System.out.println("5. Quit");
System.out.println("\n");
String option="0";
Scanner in = new Scanner(System.in);
System.out.println("\n");
option = in.nextLine();
switch (option) {
case "1":
chooseTeam();
break;
case "2":
createProfile();
break;
case "3":
loadSave();
break;
case "4":
credits();
break;
case "5":
System.out.println("Goodbye!");
default:
System.out.println("Please select an option between 1 and 4");
//menu();
} // end switch
} // end menu
} // end class
答案 0 :(得分:0)
抛出异常可能不是最好的主意,因为如果处理不当会阻止你的程序。正如Sernd建议的那样,添加默认案例似乎就像这里的简单解决方案
boolean shouldExit = false;
while (!shouldExit) {
Scanner in = new Scanner(System.in);
menu();
System.out.println("\n");
option = in.nextInt();
switch (option) {
case 1:
chooseTeam();
break;
case 2:
createProfile();
break;
case 3:
loadSave();
break;
case 4:
credits();
break;
case 5:
System.out.println("Goodbye!");
shouldExit=true;
break;
default:
System.out.println("invalid selection");
}
}