C ++代码无法正常工作?

时间:2014-02-08 20:49:14

标签: c++

对于以下代码,我遇到了问题。我希望它打印出与if语句匹配的print语句,而是打印每个if语句的所有语句。我该如何纠正这个?我是一个初学者,很抱歉这个愚蠢的问题......

#include <iostream>

using namespace std;

int main()
{
    int x;
    int y;
    int z;

    cout << "Enter your first number" << endl;
    cin >> x;

    cout << "Enter your second number" << endl;
    cin >> y;

    cout << "Enter your third number" << endl;
    cin >> z;

    ////////////Equilateral Triangle. All sides are the same/////////////
    if ((x == y) && (y == z)) {
        cout << "This is an Equilateral triangle" << endl;
    }

    ////////////Isoceles Triangle. Two sides are the same/////////////
    if ((x == y) or (y == z) or (z == x)) {
        cout << "This is an Isosceles triangle" << endl;
    }

    ////////////Scalene Triangle. All sides are different/////////////
    if ((x > y + z)or (y > x + z) or (z > x +y)) {
        cout << "Opps! That didn't work! " << endl;
    }
    else {
        cout << "This is a scalene triangle" << endl;
    }

    return 0; // exit program
}

1 个答案:

答案 0 :(得分:2)

这应该有效:

if ( x != y and y !=z and z !=x) {
        cout << "This is a scalene triangle" << endl;
    }

你也可以假设,如果它不是Equilateral或Isosceles而不是Scalene

////////////Equilateral Triangle. All sides are the same/////////////
if ((x == y) && (y == z)) {
        cout << "This is an Equilateral triangle" << endl;

}

////////////Isoceles Triangle. Two sides are the same/////////////
else if ((x == y) or (y == z) or (z == x)) {
        cout << "This is an Isosceles triangle" << endl;
}

////////////Scalene Triangle. All sides are different/////////////
else {
        cout << "This is a scalene triangle" << endl;
    }