2016-03-07 42 views
-4

我在嵌套if語句中不斷收到預期的表達式錯誤。我不明白是什麼問題,因爲我在代碼中有其他的語句格式化(就我所見),完全相同的方式不會產生錯誤!嵌套if語句中的預期表達式錯誤C++

這是用於第一年C++類中的一項任務,它根據用戶輸入數據計算保齡球分數。

#include <iostream> 

using namespace std; 

int main() { 

    // Set Variables 
    int userin_1; 
    int userin_2; 
    int userin_3; 
    int combined2_3; 
    int score; 


    // Query User 

    cout << "Welcome to Josh's Bowling Score Calculator! " << "Please enter your three scores. " 
    << "They should be between 0 and 10." << endl << endl; 

    cin >> userin_1 >> userin_2 >> userin_3; 

    // Verify input data 

    if (userin_1 <= 10 && userin_2 <=10 && userin_3 <=10) 
     userin_1 = userin_1; 
     else cout << endl << "Input Error: You cannot score greater than 10 in one turn! :|" << endl; 

    if (userin_1 >= 0 && userin_2 >=0 && userin_3 >=0) 
     userin_1 = userin_1; 
    else cout << endl << "Input Error: You cannot score less than 0 in one turn! :|" << endl; 

    if (userin_1 == 10) 
     userin_1 = userin_1; 
     else if (userin_1 + userin_2 <= 10) 
      cout << "Input Error: You can't score more than 10 on your first two throws unless your first throw is a strike! " 
      << endl; 

    if (userin_2 == 10) 
     userin_2 = userin_2; 
    else if (userin_2 + userin_3 <= 10) 
     cout << "Input Error: You can't score more than 10 on your first two throws unless your first throw is a strike! " 
     << endl; 

    // Calculate Score 

    combined2_3 = userin_2 + userin_3; 

    if (userin_1 + userin_2 < 10) 
    { 
     score = userin_1 + userin_2; 
     cout << "Because you scored less than 10 on your first two throws, your last score doesn't count :[ " 
    << "Your score is: " << score << endl; 
    } 
     else 
     { 
     if (userin_1 == 10 && userin_2 == 10 && userin_3 == 10) 
     score = userin_1 + userin_2 + userin_3; 
     cout << "Wowow 3 strikes! Your score is " << score << endl; 
     } 
      else 
      { 
      if (userin_1 == 10 && userin_2 == 10) 
      score = userin_1 + userin_2 + userin_3; 
      cout << "Two strikes! Your score is " << score << endl; 
      } 
       else 
       { 
       if (userin_1 == 10 && userin_2 + userin_3 >= 10) 
        cout << "It's not possible to score greater than 10 on your last two throws, unless your first throw is a strike :| " << endl; 
       } 
        else 
        { 
         if (userin_1 == 10 && combined2_3 >= 10) 
          score = userin_1 + userin_2 + userin_3; 
         cout << "Nice, a strike! Your score is " << score << endl; 
        } 
         else 
         { 
          if (userin_1 == 0 && userin_2 == 0 && userin_3 == 0) 
           cout << "Donny, you're out of your element. All zeroes." << endl << endl; 
         } 


    // Closing Statement 

    cout << endl << "Thanks for bowling with me, hope you have a great day"; 




} 
+3

你有(太)嵌套和不一致/不好的縮進級別。這對我們(以及大多數對您)正確縮進您的代碼會非常有幫助。 – Borgleader

+1

這不是問題,但不要使用'std :: endl',除非你需要額外的東西。 ''\ n''結束一行。 –

+1

這也不是問題,但要學習DeMorgan的定理,以便不必像'if(userin_1 <= 10 && userin_2 <= 10 && userin_3 <= 10)userin_1 = userin_1;否則無論();'。你可以這樣寫:'if(userin_1> 10 || userin_2> 10 || userin_3> 10)whatever();'。 –

回答

0

編輯:我錯了,這裏的錯誤:

else 
    { 
    if (userin_1 == 10 && userin_2 == 10 && userin_3 == 10) 
    score = userin_1 + userin_2 + userin_3; 
    cout << "Wowow 3 strikes! Your score is " << score << endl; 
    } 
     else 
     { 
     if (userin_1 == 10 && userin_2 == 10) 
     score = userin_1 + userin_2 + userin_3; 
     cout << "Two strikes! Your score is " << score << endl; 
     } 

第二else沒有收到相關if。將if的條款移到相應的else條款中。

else if (userin_1 == 10 && userin_2 == 10 && userin_3 == 10) { 
     score = userin_1 + userin_2 + userin_3; 
     cout << "Wowow 3 strikes! Your score is " << score << endl; 
    } else if (userin_1 == 10 && userin_2 == 10) { 
     score = userin_1 + userin_2 + userin_3; 
     cout << "Two strikes! Your score is " << score << endl; 
    } 

但是,這裏更嚴重的問題是您接近任務的方式。此任務的嵌套if語句的方式過多。你在條件上重複幾次,而正確的方法是隻檢查一次,然後分支。

此外,之類的語句

if (userin_1 <= 10 && userin_2 <=10 && userin_3 <=10) 
    userin_1 = userin_1; 

沒有任何意義,則應該檢查的對面,完全跳過else條款。

您還應該使用正確的縮進,因爲它極大地幫助提高了代碼的可讀性並有助於避免類似這樣的錯誤。

+0

誰留下了-1,請解釋一下。 – iksemyonov

+0

OP詢問代碼中的錯誤,我發現它。請撤銷。我認爲OP不理解if-else的作品是如何編寫的,而不是編碼風格。 – iksemyonov

+1

沒有機會。 OP可能甚至不知道他可以通過不寫這樣糟糕的代碼開始而自己發現它。你應該真的幫助他。 – Puppy