我在嵌套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();'。 –