【问题标题】:C++ Logicial OperatorsC++ 逻辑运算符
【发布时间】:2015-06-19 13:01:05
【问题描述】:

所以我编写了一个小程序来计算简单形状的周长(对于编码来说非常新,所以要保持简单)。我现在被困住了 代码太三角形了,我一生都无法弄清楚如何让最后一个逻辑运算符工作!!!?一如既往地非常感谢您的时间和建议。

最好的问候,杰克。

#include <iostream>
#include <string>
using namespace std;
int main ()
{   
    int length;
    int Diameter;
    float Pi = 3.14;
    string Shape;
    string Square = "Square";
    string Triangle = "Triangle";
    string Circle = "Circle";          

    cout <<"=======================" << endl;
    cout <<"=Welcome to Perimeters=" << endl;
    cout <<"#######################" << endl;
    cout <<"###|Select A Shape|####" << endl; 
    cout <<"=======================" << endl;
    cout <<"=   |   Circle   |    =" << endl;
    cout <<"=   |  Triangle  |    =" << endl;   
    cout <<"=   |   Square   |    =" << endl;
    cout <<"=======================" << endl;


    cout <<"Enter Shape >; ";
    cin >> Shape;

    if (Shape == "Square") {
        cout << "Enter Length of Side >: ";
        cin >> length;
        cout <<  "Perimeter = " ; 
        cout << length * 4 <<endl;
    } else {
             (Shape == "Triangle"){ 
             cout << "Enter Length of Side >: ";
             cin >> length;
             cout <<  "Perimeter = " ; 
             cout << length * 3 <<endl;
             }    

            }   
            else  {
                    (Shape == "Circle") { 
                    cout << "Enter Diameter >: ";
                    cin >> Diameter;
                    cout <<  "Perimeter = " ; 
                    cout << Diameter * Pi <<endl;
                    }

                   }




    return 0;
}

【问题讨论】:

标签: c++ if-statement logic logical-operators


【解决方案1】:

您没有正确编写 else-if 语句。格式应为:

if(boolean expression) {}
else if (boolean expression) {} // as many else ifs as you need
else {} // optional

因此你的 else if 条件应该是:

if (Shape == "Square") {
    cout << "Enter Length of Side >: ";
    cin >> length;
    cout <<  "Perimeter = " ; 
    cout << length * 4 <<endl;
} else if (Shape == "Triangle"){
    // and so on...
} else {
    cout << "Invalid shape entered.";
}

此外,PI 不是 3.14。包括&lt;math.h&gt; 并使用M_PI

【讨论】:

  • @hexafraction Awesome 在您的帮助和信息下解决了这个问题。非常感谢谢谢! #已解决
  • @marmaa 如果这解决了您的问题,而不是标签,请选中答案左侧的复选标记,将其标记为已回答您的问题。
  • 是的,我也打算这样做,但有时间限制....你实在是太快了^^
【解决方案2】:

您需要处理代码格式...

使用 else if insead of else。

if (Shape == "Square") {
    cout << "Enter Length of Side >: ";
    cin >> length;
    cout <<  "Perimeter = " ; 
    cout << length * 4 <<endl;
}
 else if (Shape == "Triangle"){ 
    cout << "Enter Length of Side >: ";
    cin >> length;
    cout <<  "Perimeter = " ; 
    cout << length * 3 <<endl;
 }    
 else if (Shape == "Circle") {
    (Shape == "Circle")
    cout << "Enter Diameter >: ";
    cin >> Diameter;
    cout <<  "Perimeter = " ; 
    cout << Diameter * Pi <<endl;
 }
 else{
    cout << "invalid shape name" << endl;
 }

【讨论】:

    【解决方案3】:

    你好你必须写 else if 如下。

     #include <iostream>
     #include <string>
    
    using namespace std;
    
    int main ()
    
    {   
         int length;
    
    int Diameter;
    float Pi = 3.14;
    string Shape;
    string Square = "Square";
    string Triangle = "Triangle";
    string Circle = "Circle";     **strong text**
    
    
    cout <<"=======================" << endl;
    cout <<"=Welcome to Perimeters=" << endl;
    cout <<"#######################" << endl;
    cout <<"###|Select A Shape|####" << endl; 
    cout <<"=======================" << endl;
    cout <<"=   |   Circle   |    =" << endl;
    cout <<"=   |  Triangle  |    =" << endl;   
    cout <<"=   |   Square   |    =" << endl;
    cout <<"=======================" << endl;
    
    
    cout <<"Enter Shape >; ";
    cin >> Shape;
    
    if (Shape == "Square") {
        cout << "Enter Length of Side >: ";
        cin >> length;
        cout <<  "Perimeter = " ; 
        cout << length * 4 <<endl;
    } else if (Shape == "Triangle"){
             cout << "Enter Length of Side >: ";
             cin >> length;
             cout <<  "Perimeter = " ; 
             cout << length * 3 <<endl;
    } else if (Shape == "Circle") { 
             cout << "Enter Diameter >: ";
             cin >> Diameter;
             cout <<  "Perimeter = " ; 
             cout << Diameter * Pi <<endl;
    }
    return 0;
    

    }

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 2014-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-02
      • 2012-11-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多