2014-02-08 157 views
-3

對於下面的代碼,我有一個問題。我希望它打印出與if語句匹配的print語句,但是它會打印每個if語句的所有語句。我該如何糾正這一點?我看到一個初學者,對愚蠢的問題,很抱歉..C++代碼無法正常工作?

#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 
} 
+0

呵呵,'(x> y + y)**或**(y> x + z)'究竟能工作多少? – Xarn

+0

你知道你可以用'!='來表示「不等於」,對吧? – olevegard

+5

'else' ................. –

回答

2

這應該工作:

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

你也可以假設,如果不是等邊或等腰三角形比它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; 
    }