我用else if语句做错了什么?

What am I doing wrong with the else if statements

本文关键字:错了 什么 语句 else if 我用      更新时间:2023-10-16

程序告诉我它需要一个带有else if语句的语句。我是新来的,在我上大学的CS课程之前,我正在努力学习c++代码。提前感谢!

#include <iostream>
#include <string>
using namespace std;
string color;
string Blue;
string Green;
string Brown;
int age;
int main()
{
    cout << "what is the color of your eyes ? (use capitalization)" << endl << "colors to choose from are " << endl << "Blue" << endl << "Green" << endl << "Brown";
    cin >> color;
    if (color == Blue); {
        cout << "you are an intelligent person " << endl;
        system("pause");
    }
    else if (color == Green); {
        cout << " you are a peaceful person " << endl;
        system("pause");
    }
    else if (color == Brown); {
        cout << "you are a normal go get 'em person " << endl;
        system("pause");
    }

    cin.ignore();
    cin.get();
    return 0;
}

你的问题是括号后面有分号。:

if (color == Blue); {
    cout << "you are an intelligent person " << endl;
    system("pause");
}

应该是这个

                 No semi colon here
                  v
if (color == Blue) {
    cout << "you are an intelligent person " << endl;
    system("pause");
}

其余的else-if

也是一样

还有,正如其他人提到的,您需要指定Blue, Green和Brown是什么。或者这样做:

const string Blue = "Blue";
const string Green = "Green";
const string Brown = "Brown";

或者:

 if (color == "Blue") {   //Note the ""
    cout << "you are an intelligent person " << endl;
    system("pause");
}

你在不属于分号的地方放了分号。您还需要为用于比较的字符串提供值:

#include <iostream>
#include <string>
using namespace std;
int main()
{
string color;
string Blue = "Blue";
string Green = "Green";
string Brown = "Brown";
    int age;
    cout << "what is the color of your eyes ? (use capitalization)" << endl << "colors to choose from are " << endl << "Blue" << endl << "Green" << endl << "Brown";
    cin >> color;
    if (color == Blue)
    {
        cout << "you are an intelligent person " << endl;
        system("pause");
    }
    else if (color == Green)
    {
        cout << " you are a peaceful person " << endl;
        system("pause");
    }
    else if (color == Brown)
    {
        cout << "you are a normal go get 'em person " << endl;
        system("pause");
    }
    cin.ignore();
    cin.get();
    return 0;
}

您需要删除if和else if语句后面的错误分号,如下所示:

if (color == Blue) {
    cout << "you are an intelligent person " << endl;
    system("pause");
}
else if (color == Green) {
    cout << " you are a peaceful person " << endl;
    system("pause");
}
else if (color == Brown) {
    cout << "you are a normal go get 'em person " << endl;
    system("pause");
}

在c++中,可以将;视为语句结束符

if (color == "Blue");这样的东西本身就是一个语句。但是它没有做任何事情,因为;终止了if语句。

此外,还可以使用大括号{}对语句进行分组。实际上,这对于限制大括号内声明的变量的作用域非常有用。您将在适当的时候发现这对程序稳定性的积极影响。

长话短说,一旦修复了可疑的字符串比较(您需要像我上面所做的那样对字面量使用引号字符),您的代码在语法上是完全有效的。这就是它编译的原因。(尽管一个好的编译器应该警告你空的if控制块)。但是这并不是您想要的:您需要删除所有包含if的行上多余的;

除了额外的分号之外,您还没有实际初始化字符串值,因此它们都是空字符串。您需要在开头添加:

Blue = "Blue";
Green = "Green";
Brown = "Brown";

您的变量Blue, GreenBrown是空字符串变量。

string Blue; // empty string with name Blue
string Green; // empty string with name Green
string Brown; // empty string with name Brown

首先,您需要删除错误的分号,并且需要使用带内容的字符串。您可以省略变量并使用字符串字面值:

if (color == "Blue") {

或者您需要在字符串中填充一些数据:

std::string Blue("Blue");
// ...
if (color == Blue); {