我有以下代码,我收到一个无法访问的语句错误被抛出。该行有一条评论说它是罪魁祸首。
public static void selectPlayer ()
{
// Loops through Player class' static array. If an index is storing a player object then print the player name out with a number for selection.
for(int i=0; 1<101; i++)
{
if (Player.playerArray[i] != null)
{
System.out.println(i + ". " + Player.playerArray[i - 1].playerName);
}
System.out.print("\nPlease enter the number that corresponds to the player you wish to use; "); // This line is where it failed to compile a la unreachable statement.
Scanner scanner = new Scanner(System.in);
// Take players selection and minus 1 so that it matches the Array index the player should come from.
int menuPlayerSelection = scanner.nextInt() - 1;
// Offer to play a new game with selected player, view player's stats, or exit.
System.out.print("You have selected " + Player.playerArray[menuPlayerSelection].playerName + ".\n1) Play\n2) View Score\n3) Exit?\nWhat do you want to do?: ");
int areYouSure = scanner.nextInt();
// Check player's selection and run the corresponding method/menu
switch (areYouSure)
{
case 1: MainClass.playGame(menuPlayerSelection); break;
case 2: MainClass.viewPlayerScore(menuPlayerSelection); break;
case 3: MainClass.firstMenu(); break;
default: System.out.println("Invalid selection, please try again.\n");
MainClass.firstMenu();
}
}
我的问题是,我该如何解决?我明白为什么通常会发生无法访问的语句错误,但我无法弄清楚为什么它会发生在我的情况下。
答案 0 :(得分:7)
首先编辑此内容。
for(int i=0; 1<101; i++) {
无限循环。 说我而不是1.
for(int i=0; i<101; i++){
答案 1 :(得分:2)
查看你的for循环的条件。它永远不会结束。
答案 2 :(得分:1)
for(int i=0; 1<101; i++)
应该是
for(int i=0; i<101; i++)
您的病情是1 <101。无限循环总是如此。
答案 3 :(得分:1)
您的代码缺少}
,肯定会导致其中断
正如其他人所说,你的for循环
for(int i=0; 1<101; i++)
始终符合条件,看起来您将开始获得IndexOutOfBound
例外。
答案 4 :(得分:0)
你有for(int i=0; 1<101; i++)
这是一个无限循环(因为1总是低于101)。