【问题标题】:This code is getting unreachable code, and I'm not sure why此代码正在获取无法访问的代码,我不知道为什么
【发布时间】:2020-06-12 02:42:01
【问题描述】:

抱歉,代码量很大,但我不知道为什么

int timesWon;

在第 90 行出现无法访问的代码错误。 放在第 90 行的任何东西都是无法访问的代码,这意味着它之后的任何东西都不可读。

这是我的任务的掷骰子游戏代码:

package homework2_3;

import java.security.*;

public class Craps 
{
    static enum score
    {
        win, lose
    }
    public static void main(String[] args)
    {
        //random Number for a dice roll
        SecureRandom random = new SecureRandom();
        //ints for the totals on the two dice
        int dice1;
        int dice2;
        //array for times won/lost
        score[] total = new score[1000000];
        //int for the score of the first throw, if it was not an imediate win or loss
        int throw1Score = 0;

        //count how many times a win or loss happened at each roll from 1-21
        int[] rollWon = new int[22];
        int[] rollLost = new int[22];

        //loop for each game from 1-1000000
        for(int indexGame = 1; 1 <= 1000000; indexGame++)
        {
            //loop for each throw within a game
            for(int indexThrow = 1; total[indexGame] != score.win || total[indexGame] != score.lose; indexThrow++)
            {
                //get the total of blips on the dice
                dice1 = random.nextInt(6) + 1;
                dice2 = random.nextInt(6) + 1;
                //check if the throw total in throw 1
                if(indexThrow == 1)
                {
                    //check if throw 1 is an instant win
                    if((dice1 + dice2) == 7 || (dice1 + dice2) == 11)
                    {
                        total[indexGame] = score.win;
                        rollWon[indexThrow]++;
                    }
                    //check if throw 1 is an instant loss
                    else if((dice1 + dice2) == 2 || (dice1 + dice2) == 3 || (dice1 + dice2) == 12)
                    {
                        total[indexGame] = score.lose;
                        rollLost[indexThrow]++;
                    }
                    //get your "point"
                    else
                    {
                        throw1Score = dice1 + dice2;
                    }
                }
                //anything other than throw 1
                else
                {
                    //check if you "made your point"
                    if((dice1 + dice2) == throw1Score)
                    {
                        total[indexGame] = score.win;
                        if(indexThrow <= 20)
                        {
                            rollWon[indexThrow]++;
                        }
                        else if(indexThrow > 20)
                        {
                            rollWon[21]++;
                        }
                    }
                    //check if you rolled a 7 (lost)
                    else if((dice1 + dice2) == 7)
                    {
                        total[indexGame] = score.lose;
                        if(indexThrow <= 20)
                        {
                            rollLost[indexThrow]++;
                        }
                        else if(indexThrow > 20)
                        {
                            rollLost[21]++;
                        }
                    }
                }
            }
        }
        //ints to add up all the wins and losses in the array of scores
        int timesWon;
        int timesLost;
        //loop for the adding
        for(int winLossCheck = 1; winLossCheck <= 1000000; winLossCheck++)
        {
            if(total[winLossCheck] == score.win)
            {
                timesWon++;
            }
            if(total[winLossCheck] == score.lose)
            {
                timesLost++;
            }
        }
        //print the total times you won/lost
        System.out.println("you won " + timesWon + " times, and you lost " + timesLost + " times");
    }
}

据我所知,一切在逻辑上和语法上都是正确的。

提前感谢您的帮助!

【问题讨论】:

  • for(int indexGame = 1; 1 &lt;= 1000000; indexGame++) 是一个无限循环,1 永远小于 1000000。也许你的意思是indexGame &lt;= 1000000
  • 注意枚举是类型,应该用“Pascal case”(Score)命名,枚举值是常量,应该大写(WIN, LOSE)。

标签: java compiler-errors unreachable-code


【解决方案1】:

1 &lt;= 1000000 始终是true

【讨论】:

    【解决方案2】:

    您有一个条件为1 &lt;= 1000000 的for 循环。该循环不会退出,从而使循环之后的所有代码都无法访问。将条件更改为代码将退出的条件。

    例如:

    for(int i = 1; i<=10; i++) {
       System.out.println(i);
    }
    

    此代码将打印出 1-10 的整数。但是,如果我创建另一个 for 循环,其条件始终为真,例如 0

    【讨论】:

      猜你喜欢
      • 2018-09-28
      • 2012-04-11
      • 2020-08-27
      • 1970-01-01
      • 1970-01-01
      • 2023-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多