继续收到错误"并非所有代码路径都返回一个值"

时间:2016-04-08 02:46:12

标签: c#

所以基本上我试图做一个问答游戏,这个子程序检查答案是否正确。并且基本上每次我尝试运行程序时都会给我一个错误,即并非所有代码路径都返回一个值,即使它清楚地看到有一个返回true;并且返回虚假;最后的陈述。

private bool CheckIfCorrect(byte questionNum, string answers, string userAnswer)
    {
        int adjustmentToTheScore;

        const int EASY_QUESTION_5 = 1;
        const int MEDIUM_QUESTION_10 = 2;
        const int HARD_QUESTION_15 = 3;
        const int GENERALPOINTS = 100;

        if (userAnswer == "A" || userAnswer == "B" || userAnswer == "C" || userAnswer == "D")
        {
            if (answers == userAnswer)
            {
                if (questionNum <= EASY_QUESTION_5)
                {
                    adjustmentToTheScore = (EASY_QUESTION_5 * GENERALPOINTS / totalTimePassed);

                    userScore += adjustmentToTheScore;
                }
                else if (questionNum <= MEDIUM_QUESTION_10)
                {
                    adjustmentToTheScore = (MEDIUM_QUESTION_10 * GENERALPOINTS / totalTimePassed);

                    userScore += adjustmentToTheScore;
                }
                else if (questionNum <= HARD_QUESTION_15)
                {
                    adjustmentToTheScore = (HARD_QUESTION_15 * GENERALPOINTS / totalTimePassed);

                    userScore += adjustmentToTheScore;
                }
                rightAnswerCount++;

                goalSound.SoundLocation = "Goal_Sound.wav";
                goalSound.Play();

                lblTotalCorrect.Text = Convert.ToString(rightAnswerCount);

                if (fastestAnswer == 0 || totalTimePassed < fastestAnswer)
                {
                    fastestAnswer = totalTimePassed;

                    lblFastestAnswer.Text = Convert.ToString(fastestAnswer) + "(s)";
                }

           else
                {
                    adjustmentToTheScore = 10;

                    userScore = userScore - adjustmentToTheScore;

                    booing.SoundLocation = "Booing.wav";
                    booing.Play();
                }

                lblScore.Text = "Score: " + Convert.ToString(userScore);

                return true;
            }
            else
            {
                MessageBox.Show("Invalid answer please put a, b, c or d!");

                return false;
            }

        }
    }
}

}

6 个答案:

答案 0 :(得分:0)

尝试在最后return false语句后添加else

答案 1 :(得分:0)

如果输入第一个if语句,则只返回true或false。如果第一个return失败,则底部需要if

答案 2 :(得分:0)

如果第一个条件if (userAnswer == "A" || userAnswer == "B" || userAnswer == "C" || userAnswer == "D")的计算结果为false,则需要指定程序要执行的操作。因为该方法应为所有方案返回类型bool的值。因此,您需要做的是在return之后添加if语句,truefalse由您决定。所以代码片段看起来像:

private bool CheckIfCorrect(byte questionNum, string answers, string userAnswer)
    {
       //init here
        if (userAnswer == "A" || userAnswer == "B" || userAnswer == "C" || userAnswer == "D")
        {
            if (answers == userAnswer)
            {
                if (questionNum <= EASY_QUESTION_5)
                {
                    //Some code
                }
                else if (questionNum <= MEDIUM_QUESTION_10)
                {
                    //Some code
                }
                else if (questionNum <= HARD_QUESTION_15)
                {
                    //Some code
                }
                if (true)
                {
                    //Some code
                }

                else
                {
                    //Some code
                }
                return true;
            }
            else
            {
                return false;
            }
        }
        return false;
  }  

答案 3 :(得分:0)

该错误意味着您有返回语句是有条件的,并且代码可能永远不会达到这些条件,因此它不会返回值。

只需剪切return false;部分,然后将其贴在第二个花括号下面的位置之后。

您仍会收到消息框,但仍会返回false。它看起来是true以上的唯一条件。返回语句结束方法范围的评估。因此,return true下方的任何内容都会在点击该行后进行评估。

答案 4 :(得分:0)

在非void函数中,程序/方法中的每个逻辑路径都应该结束向调用者返回一个值。

在您的情况下,首先,如果阻止错误的条件不会返回任何内容,这会导致exception

private bool CheckIfCorrect(byte questionNum, string answers, string userAnswer)
{
    int adjustmentToTheScore;

    const int EASY_QUESTION_5 = 1;
    const int MEDIUM_QUESTION_10 = 2;
    const int HARD_QUESTION_15 = 3;
    const int GENERALPOINTS = 100;

    if (userAnswer == "A" || userAnswer == "B" || userAnswer == "C" || userAnswer == "D")
    {
        if (answers == userAnswer)
        {
            if (questionNum <= EASY_QUESTION_5)
            {
                adjustmentToTheScore = (EASY_QUESTION_5 * GENERALPOINTS / totalTimePassed);

                userScore += adjustmentToTheScore;
            }
            else if (questionNum <= MEDIUM_QUESTION_10)
            {
                adjustmentToTheScore = (MEDIUM_QUESTION_10 * GENERALPOINTS / totalTimePassed);

                userScore += adjustmentToTheScore;
            }
            else if (questionNum <= HARD_QUESTION_15)
            {
                adjustmentToTheScore = (HARD_QUESTION_15 * GENERALPOINTS / totalTimePassed);

                userScore += adjustmentToTheScore;
            }
            rightAnswerCount++;

            goalSound.SoundLocation = "Goal_Sound.wav";
            goalSound.Play();

            lblTotalCorrect.Text = Convert.ToString(rightAnswerCount);

            if (fastestAnswer == 0 || totalTimePassed < fastestAnswer)
            {
                fastestAnswer = totalTimePassed;

                lblFastestAnswer.Text = Convert.ToString(fastestAnswer) + "(s)";
            } else
            {
                adjustmentToTheScore = 10;
                userScore = userScore - adjustmentToTheScore;

                booing.SoundLocation = "Booing.wav";
                booing.Play();
            }

            lblScore.Text = "Score: " + Convert.ToString(userScore);
            return true;
        }
        else
        {
            MessageBox.Show("Invalid answer please put a, b, c or d!");
            return false;
        }
    }

    return false; // this is required.
}

答案 5 :(得分:0)

    private bool CheckIfCorrect(byte questionNum, string answers, string userAnswer)
        {
            int adjustmentToTheScore;

            const int EASY_QUESTION_5 = 1;
            const int MEDIUM_QUESTION_10 = 2;
            const int HARD_QUESTION_15 = 3;
            const int GENERALPOINTS = 100;

            if (userAnswer == "A" || userAnswer == "B" || userAnswer == "C" || userAnswer == "D")
            {
                if (answers == userAnswer)
                {
                    if (questionNum <= EASY_QUESTION_5)
                    {
                        adjustmentToTheScore = (EASY_QUESTION_5 * GENERALPOINTS / totalTimePassed);

                        userScore += adjustmentToTheScore;
                    }
                    else if (questionNum <= MEDIUM_QUESTION_10)
                    {
                        adjustmentToTheScore = (MEDIUM_QUESTION_10 * GENERALPOINTS / totalTimePassed);

                        userScore += adjustmentToTheScore;
                    }
                    else if (questionNum <= HARD_QUESTION_15)
                    {
                        adjustmentToTheScore = (HARD_QUESTION_15 * GENERALPOINTS / totalTimePassed);

                        userScore += adjustmentToTheScore;
                    }
                    rightAnswerCount++;

                    goalSound.SoundLocation = "Goal_Sound.wav";
                    goalSound.Play();

                    lblTotalCorrect.Text = Convert.ToString(rightAnswerCount);

                    if (fastestAnswer == 0 || totalTimePassed < fastestAnswer)
                    {
                        fastestAnswer = totalTimePassed;

                        lblFastestAnswer.Text = Convert.ToString(fastestAnswer) + "(s)";
                    }

               else
                    {
                        adjustmentToTheScore = 10;

                        userScore = userScore - adjustmentToTheScore;

                        booing.SoundLocation = "Booing.wav";
                        booing.Play();
                    }

                    lblScore.Text = "Score: " + Convert.ToString(userScore);

                    return true;
                }
                else
                {
                    MessageBox.Show("Invalid answer please put a, b, c or d!");

                    return false;
                }

            }
            else  
            {
            return false; --- add for first if condition
            }
    }