Java 无法到达的声明-为什么在这里?
我有下面的代码,我得到一个无法访问的语句错误被抛出。这条有问题的线路在说它是罪魁祸首后有一条评论Java 无法到达的声明-为什么在这里?,java,Java,我有下面的代码,我得到一个无法访问的语句错误被抛出。这条有问题的线路在说它是罪魁祸首后有一条评论 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;
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();
}
}
publicstaticvoidselectplayer()
{
//循环浏览玩家类的静态数组。如果索引存储玩家对象,则打印玩家名称和数字以供选择。
对于(inti=0;1首先编辑此
for(int i=0; 1<101; i++) {
for(int i=0;1查看for循环的条件。它永远不会结束。for(int i=0;1您有for(int i=0;1您的代码缺少一个}
,这肯定会导致它中断
for(int i=0; 1<101; i++)
正如其他人所说,您的for循环
for(int i=0; 1<101; i++)
for(int i=0;1但1始终小于101,因此条件始终为true->infinite循环。此编码样式是内联{
之后的下一个逻辑步骤。为什么不使用内联}
缺少一个右括号(}),但可能只是stackoverflow的编辑…我不敢相信我错过了。看起来很难用我的字体看出I和1之间的区别。干杯,非常感谢。@RudiKershaw您也忘了关闭for循环括号。虽然这是真的,但这并不能解释为什么有问题的行被认为是无法到达的。
for(int i=0; i<101; i++)
for(int i=0; 1<101; i++)