2016-04-08 38 views
-1

所以基本上我試圖做一個測驗遊戲,這個子程序檢查答案是否正確。基本上每次我嘗試運行程序時都會給我一個錯誤,即並不是所有的代碼路徑都返回一個值,即使它清楚地看到有一個返回值爲真;並返回錯誤;聲明在最後。繼續收到錯誤「不是所有的代碼路徑都返回一個值」

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

     } 
    } 
} 

}

+0

嵌套減少代碼的可讀性,壞習慣這種方式代碼..靜態代碼分析工具會爲您提供建議,以便改進您的代碼... [請參閱此處](http://stackoverflow.com/questions/268132/invert-if-statement-to-reduce-nesting)關於此場景的一些好的討論。 – felickz

回答

0

如果第一個條件if (userAnswer == "A" || userAnswer == "B" || userAnswer == "C" || userAnswer == "D")的計算結果爲false,則需要指定程序想要執行的操作。因爲該方法應該爲所有場景返回類型爲bool的值。因此,您需要做的是在if之後添加return陳述,它可以是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; 
    } 
0

嘗試最後else語句之後添加return false

0

如果您輸入第一個if語句,則只返回true或false。如果第一個if失敗,則需要底部的return

0

該錯誤意味着你必須是有條件的返回語句,它有可能是代碼永遠達不到這些條件,因此它不會返回一個值。

只需剪下return false;部分,然後將其粘貼在當前位置下方的第二個大括號內。

您仍然會收到消息框,它仍會返回false。它看起來像是true以上的唯一條件。返回語句結束對方法範圍的評估。因此,在return true下面的任何內容都不會被評估。

0

在一個非void函數中,程序/方法中的每個邏輯路徑應該 end將值返回給調用者。

在你的情況,如果首塊的假條件不返回任何東西,這是造成條件語句的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. 
} 
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 
      } 
    } 
相關問題