[英]C++ Nested if else statement help (error: expected '}' before 'else')
[英]expected expression error in nested if statement c++
我在嵌套的 if else 語句中不斷收到預期的表達式錯誤。 我不明白問題是什么,因為我在代碼中還有其他語句,它們的格式(據我所知)與不會產生錯誤的方式完全相同!
這是針對第一年 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";
}
編輯:我錯了,這是錯誤:
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
子句。
您還應該使用適當的縮進,因為它極大地有助於提高代碼可讀性並有助於避免此類錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.