简体   繁体   English

此代码是无法访问的代码,我不知道为什么

[英]This code is getting unreachable code, and I'm not sure why

Sorry for the large amount of code, but I'm not sure why抱歉有大量代码,但我不知道为什么

int timesWon;

is getting a unreachable code error on line 90. Anything that is put on line 90 is unreachable code, meaning anything after it isn't readable.在第 90 行出现无法访问的代码错误。放在第 90 行的任何内容都是无法访问的代码,这意味着它之后的任何内容都不可读。

This is my code for a game of craps for an assignment:这是我为任务进行掷骰子游戏的代码:

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

As far as I can tell, everything is logically correct and syntactically correct.据我所知,一切在逻辑上和语法上都是正确的。

Thanks in advance for any help!在此先感谢您的帮助!

1 <= 1000000始终为true

You have a for loop in which the condition is 1 <= 1000000 .您有一个 for 循环,其中条件为1 <= 1000000 This loop will not exit, thus making all code after the loop unreachable.此循环不会退出,从而使循环后的所有代码都无法访问。 Change the the condition to something where the code will exit.将条件更改为代码将退出的内容。

For example:例如:

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

This code will print out the integers from 1-10.此代码将打印出 1-10 之间的整数。 However if I create another for loop with a condition that is always true like 0<7.但是,如果我创建另一个 for 循环,其条件始终为真,例如 0<7。 Since the loop won't end, all code after it is unreachable.由于循环不会结束,它之后的所有代码都无法访问。 Change your for loop to have a condition that won't always be true, so the program will continue.更改您的 for 循环以使其条件不总是为真,因此程序将继续。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法访问的代码; 我不明白为什么它无法访问 - Unreachable code ; i dont understand why it is unreachable 不知道为什么我会收到NullPointerException错误 - Not sure why I'm getting a NullPointerException error 不确定为什么我会收到 EOFException - Not sure why I'm getting an EOFException 为什么我在 java 代码中遇到无法访问的语句错误。 需要帮助来解决这个错误 - why am I getting unreachable statement error in java code. need help to solve this error 为什么此代码显示“无法访问的代码”错误? 我该如何解决? - Why this code is showing “unreachable code” error?? And how can I fix it? 有人可以解释为什么我在这段代码中收到错误 - Can someone please explain why I'm getting an error in this code 为什么在此代码中我没有从服务器得到任何响应 - Why I'm not getting any response from server in this code 为什么会出现“无法访问的代码”和“变量未初始化”的编译错误? - Why do I get an “unreachable code” and “variable not initialized” compilation error? 为什么这段代码会给出“无法访问的代码”错误? - Why is this code giving an “unreachable code” error? 为什么在这个代码序列中出现 Unreachable code 错误而不是后者? - Why is an Unreachable code error in this sequence of code and not the latter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM