繁体   English   中英

嵌套if语句c ++中的预期表达式错误

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM