我在嵌套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";
}
你有(太)嵌套和不一致/不好的縮進級別。這對我們(以及大多數對您)正確縮進您的代碼會非常有幫助。 – Borgleader
這不是問題,但不要使用'std :: endl',除非你需要額外的東西。 ''\ n''結束一行。 –
這也不是問題,但要學習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();'。 –