Этот код получает недоступный код, и я не уверен, почему - PullRequest
0 голосов
/ 28 февраля 2020

Извините за большой объем кода, но я не уверен, почему

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");
    }
}

Насколько я могу судить, все логически правильно и синтаксически правильно.

Заранее спасибо за любую помощь!

Ответы [ 2 ]

1 голос
/ 28 февраля 2020

1 <= 1000000 всегда true

0 голосов
/ 28 февраля 2020

У вас есть для l oop, в котором условие 1 <= 1000000. Это l oop не завершится, поэтому весь код после l oop будет недоступен. Измените условие на то, из которого выйдет код.

Например:

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

Этот код выведет целые числа от 1 до 10. Однако, если я создам другое для l oop с условием, которое всегда верно, например, 0 <7. Поскольку l oop не заканчивается, весь код после него недоступен. Измените для l oop условие, которое не всегда будет истинным, поэтому программа продолжится. </p>

...