为什么我的计算器不响应我的 if 语句?

Why is my calculator not responding to my if statements?

这里是新手程序员。我完成了我正在处理的这个项目,并且我的所有 ifelse 语句都正常运行,但只有当它们被放置在我正在编写的代码的最顶部时。

例如,我从 if(number == 1) 开始编写这段代码,然后一路往下写,但我最终不得不将它们剪切并粘贴到顶部,因为当我输入的值不是 1 (第一个 if 语句,因此是起作用的 top 语句),计算器在输入除顶部以外的任何其他值时根本无法打印任何内容。因为 4 在顶部,所以它可以正常工作,但是 1-3 不起作用,如果我将 1 放在顶部,那么 2-4 将不会打印任何内容。

下面是底部的 if 语句的屏幕截图,没有打印,也没有调试标志:

我确定这是一个小问题,我设法把它变成了大问题。

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    int number;
    float area1, area2, area3, area4;
    int R1;
    int trib;
    int trih;
    int top;
    int bott;
    int traph;
    int majxis;
    int minxis;
    const float PI= 3.14159;
    cout<< "I'm starting my program ";
    cout<<"\n1.Circle";
    cout<<"\n2.triangle";
    cout<<"\n3.trapezoid";
    cout<<"\n4.ellipse ";
    cout<<"\nEnter the corresponding number of your choice(1-4):";
    cin>> number;
    if(number <= 0)
    {
        cout<<"Integer is invalid, try again ";
        cin>> number;
    }
    if(number == 4)
    {
        cout<<"\nWhat is the length of the semi-major axis of the ellipse? ";
    }
     cin>> majxis;
     if(majxis <=0)
     {
         cout<<"Invalid value for the semi-major axis, try again: ";
     }
     else
     {
         cout<<"What is the length for the semi-minor axis of the ellipse? ";
     }
     cin>> minxis;
     area4= PI*minxis*majxis;
     if(minxis <=0)
     {
         cout<<"Invalid value for the semi-minor axis, try again: ";
     }
     else
     {
         cout<<"The area of an ellipse with a semi-major axis of " << majxis<< "and a semi-minor axis of " << minxis << "is " << area4;
     }
    if(number == 3)
    {
        cout<<"\nWhat is the length of the top of the trapezoid? ";
    }
     cin>> top;
    
     if(top <=0 )
     {
         cout<<"\n Invalid value for top length of trapezoid, try again";
     }
     else
     {
         cout<<"\nWhat is the length of the bottom of the trapezoid? ";
     }
     cin>> bott;
     if (bott <= 0)
     {
         cout<<"Invalid value for bottom length of trapezoid, try again: ";
     }
     else
     {
         cout<<"What is the height of the trapezoid? ";
     }
     cin>>traph;
     area3 = (top+bott)*0.5+traph;
     if (traph <= 0)
     {
         cout<<"Invalid value for height of trapezoid";
     }
     else
     {
         cout<<"The area of a trapezoid with a bottom of " << bott << "and a top of " << top << "is " << area3;
     }
    if(number == 2)
    {
        cout<<"\nWhat is the length of the base of the triangle?";
    }
    cin>> trib;
    if(trib<=0)
    {
        cout<<"\nInvalid Base of triangle, try again:";
    }
    else
    {
        cout<<"\nWhat is the height of the triangle? ";
    }
    cin>> trih;
    area2 = (0.5*trib)*trih;
    if(trih<=0)
    {
        cout<<"\nInvalid height of triangle, try again:";
    }
    else
    {
        cout<<"\nThe area of a trangle with a base of " << trib << "and a height of "<< trih <<" is " << area2;
    }
    if(number == 1)
    {
        cout<<"\nWhat is the radius of the circle?";
    }
    cin>> R1;
    area1 = PI*(R1 *R1);
    if( R1 <=0)
    {cout<<"\nRadius must be greater than 0, try again";
        cin>> R1;
    }
    else
    {
        cout<< "\nThe area of a circle with a radius of " << R1 << " is" << area1;
    }
}

确实是个小问题。这是正确的程序:

#include <iostream>
#include <iomanip>

int main()
{
    int number;
    float area1, area2, area3, area4;
    int R1;
    int trib;
    int trih;
    int top;
    int bott;
    int traph;
    int majxis;
    int minxis;
    const float PI = 3.14159;

    std::cout << "I'm starting my program ";
    std::cout << "\n1.Circle";
    std::cout << "\n2.triangle";
    std::cout << "\n3.trapezoid";
    std::cout << "\n4.ellipse ";
    std::cout << "\nEnter the corresponding number of your choice(1-4):";
    std::cin >> number;

    if (number <= 0)
    {
        std::cout << "Integer is invalid, try again ";
        std::cin >> number;
    }
    else if (number == 1)
    {
        std::cout << "\nWhat is the radius of the circle?";
        
        std::cin >> R1;
        area1 = PI * (R1 * R1);
        if (R1 <= 0)
        {
            std::cout << "\nRadius must be greater than 0, try again";
            std::cin >> R1;
        }
        else
        {
            std::cout << "\nThe area of a circle with a radius of " << R1 << " is" << area1;
        }
    }
    else if (number == 2)
    {
        std::cout << "\nWhat is the length of the base of the triangle?";

        std::cin >> trib;
        if (trib <= 0)
        {
            std::cout << "\nInvalid Base of triangle, try again:";
        }
        else
        {
            std::cout << "\nWhat is the height of the triangle? ";
        }
        std::cin >> trih;
        area2 = (0.5 * trib) * trih;
        if (trih <= 0)
        {
            std::cout << "\nInvalid height of triangle, try again:";
        }
        else
        {
            std::cout << "\nThe area of a trangle with a base of " << trib << "and a height of " << trih << " is " << area2;
        }
    }
    else if (number == 3)
    {
        std::cout << "\nWhat is the length of the top of the trapezoid? ";

        std::cin >> top;

        if (top <= 0)
        {
            std::cout << "\n Invalid value for top length of trapezoid, try again";
        }
        else
        {
            std::cout << "\nWhat is the length of the bottom of the trapezoid? ";
        }
        std::cin >> bott;
        if (bott <= 0)
        {
            std::cout << "Invalid value for bottom length of trapezoid, try again: ";
        }
        else
        {
            std::cout << "What is the height of the trapezoid? ";
        }
        std::cin >> traph;
        area3 = (top + bott) * 0.5 + traph;
        if (traph <= 0)
        {
            std::cout << "Invalid value for height of trapezoid";
        }
        else
        {
            std::cout << "The area of a trapezoid with a bottom of " << bott << "and a top of " << top << "is " << area3;
        }
    }
    else if (number == 4)
    {
        std::cout << "\nWhat is the length of the semi-major axis of the ellipse? ";

        std::cin >> majxis;
        if (majxis <= 0)
        {
            std::cout << "Invalid value for the semi-major axis, try again: ";
        }
        else
        {
            std::cout << "What is the length for the semi-minor axis of the ellipse? ";
        }
        std::cin >> minxis;
        area4 = PI * minxis * majxis;
        if (minxis <= 0)
        {
            std::cout << "Invalid value for the semi-minor axis, try again: ";
        }
        else
        {
            std::cout << "The area of an ellipse with a semi-major axis of " << majxis << "and a semi-minor axis of " << minxis << "is " << area4;
        }
    }
}

解释:我们以eclipse的区域(number == 4)为例。在你的代码中,你做了以下 if 条件:

if(number == 4)
{
    cout<<"\nWhat is the length of the semi-major axis of the ellipse? ";
}

这意味着 if number == 4 then print - "\n椭圆的 semi-major 轴的长度是多少?" 但它之后的所有代码都在 if 条件的 {} 括号之外,因此不被视为 if 语句的一部分。即使数字 != 4,它也会被调用。现在,在我的代码中,我已经这样做了:

else if (number == 4)
{
    std::cout << "\nWhat is the length of the semi-major axis of the ellipse? ";

    std::cin >> majxis;
    if (majxis <= 0)
    {
        std::cout << "Invalid value for the semi-major axis, try again: ";
    }
    else
    {
        std::cout << "What is the length for the semi-minor axis of the ellipse? ";
    }
    std::cin >> minxis;
    area4 = PI * minxis * majxis;
    if (minxis <= 0)
    {
        std::cout << "Invalid value for the semi-minor axis, try again: ";
    }
    else
    {
        std::cout << "The area of an ellipse with a semi-major axis of " << majxis << "and a semi-minor axis of " << minxis << "is " << area4;
    }
}

在这里,所有与eclipse区域相关的代码都是if (number == 4)语句的一部分,因为它存在于if (number == 4)语句的括号{}之间.

将此应用于所有 4 种情况,您将获得正确的程序。

此外,最好重构你的代码:

#include <iostream>
#include <iomanip>

const float PI = 3.14159;

void areaof_circle()
{
    float area;
    int R1;

    std::cout << "\nWhat is the radius of the circle?";

    std::cin >> R1;
    area = PI * (R1 * R1);
    if (R1 <= 0)
    {
        std::cout << "\nRadius must be greater than 0, try again";
        std::cin >> R1;
    }
    else
    {
        std::cout << "\nThe area of a circle with a radius of " << R1 << " is" << area;
    }
}

void areaof_triangle()
{
    float area;
    int trib, trih;
    std::cout << "\nWhat is the length of the base of the triangle?";

    std::cin >> trib;
    if (trib <= 0)
    {
        std::cout << "\nInvalid Base of triangle, try again:";
    }
    else
    {
        std::cout << "\nWhat is the height of the triangle? ";
    }
    std::cin >> trih;
    area = (0.5 * trib) * trih;
    if (trih <= 0)
    {
        std::cout << "\nInvalid height of triangle, try again:";
    }
    else
    {
        std::cout << "\nThe area of a trangle with a base of " << trib << "and a height of " << trih << " is " << area;
    }
}

void areaof_trapezoid()
{
    float area;
    int top, bott, traph;

    std::cout << "\nWhat is the length of the top of the trapezoid? ";

    std::cin >> top;

    if (top <= 0)
    {
        std::cout << "\n Invalid value for top length of trapezoid, try again";
    }
    else
    {
        std::cout << "\nWhat is the length of the bottom of the trapezoid? ";
    }
    std::cin >> bott;
    if (bott <= 0)
    {
        std::cout << "Invalid value for bottom length of trapezoid, try again: ";
    }
    else
    {
        std::cout << "What is the height of the trapezoid? ";
    }
    std::cin >> traph;
    area = (top + bott) * 0.5 + traph;
    if (traph <= 0)
    {
        std::cout << "Invalid value for height of trapezoid";
    }
    else
    {
        std::cout << "The area of a trapezoid with a bottom of " << bott << "and a top of " << top << "is " << area;
    }
}

void areaof_ellipse()
{
    float area;
    int majxis, minxis;

    std::cout << "\nWhat is the length of the semi-major axis of the ellipse? ";

    std::cin >> majxis;
    if (majxis <= 0)
    {
        std::cout << "Invalid value for the semi-major axis, try again: ";
    }
    else
    {
        std::cout << "What is the length for the semi-minor axis of the ellipse? ";
    }
    std::cin >> minxis;
    area = PI * minxis * majxis;
    if (minxis <= 0)
    {
        std::cout << "Invalid value for the semi-minor axis, try again: ";
    }
    else
    {
        std::cout << "The area of an ellipse with a semi-major axis of " << majxis << "and a semi-minor axis of " << minxis << "is " << area;
    }
}

int main()
{
    int number;
    const float PI = 3.14159;

    std::cout << "I'm starting my program ";
    std::cout << "\n1.Circle";
    std::cout << "\n2.triangle";
    std::cout << "\n3.trapezoid";
    std::cout << "\n4.ellipse ";
    std::cout << "\nEnter the corresponding number of your choice(1-4):";
    std::cin >> number;

    if (number <= 0)
    {
        std::cout << "Integer is invalid, try again ";
        std::cin >> number;
    }
    else if (number == 1)
    {
        areaof_circle();
    }
    else if (number == 2)
    {
        areaof_triangle();
    }
    else if (number == 3)
    {
        areaof_trapezoid();
    }
    else if (number == 4)
    {
        areaof_ellipse();
    }
}

此外,请勿在您的程序中使用以下内容:

using namespace std;

...因为它被认为是不好的做法。每次都使用 std::。

你的 if 语句有问题.. 由于已经给出了定义明确的答案,我正在简化您的代码的可读性并建议更好的方法来做到这一点..

注意:考虑到你是新手。

创建函数来处理不同的四边形

#include <iostream>
#include <iomanip>
using namespace std;

    float area1, area2, area3, area4;
    int R1;
    int trib;
    int trih;
    int top;
    int bott;
    int traph;
    int majxis;
    int minxis;
    const float PI= 3.14159;

void handle_circle();
void handle_ellipse();
void handle_triangle();
void handle_trapizoid();

int main()
{
    int number;
    cout<< "I'm starting my program ";
    cout<<"\n1.Circle";
    cout<<"\n2.triangle";
    cout<<"\n3.trapezoid";
    cout<<"\n4.ellipse ";
    cout<<"\nEnter the corresponding number of your choice(1-4):";
    cin>> number;
    if(number <= 0)
    {
        cout<<"Integer is invalid, try again ";
        cin>> number;
    }
    if(number == 4)
    {
        handle_ellipse();
    }
    if(number == 3)
    {
        handle_trapizoid();
    }
    if(number == 2)
    {
        handle_triangle();
    }
    
    if(number == 1)
    {
        handle_circle();
    }
}

void handle_trapizoid()
{
         cout<<"\nWhat is the length of the top of the trapezoid? ";
     cin>> top;
    
    
     if(top <=0 )
     {
         cout<<"\n Invalid value for top length of trapezoid, try again";
     }
     else
     {
         cout<<"\nWhat is the length of the bottom of the trapezoid? ";
     cin>> bott;
     }
    
    if (bott <= 0)
     {
         cout<<"Invalid value for bottom length of trapezoid, try again: ";
     }
     else
     {
         cout<<"What is the height of the trapezoid? ";
     cin>>traph;
     area3 = (top+bott)*0.5+traph;
     }
     if (traph <= 0)
     {
         cout<<"Invalid value for height of trapezoid";
     }
     else
     {
         cout<<"The area of a trapezoid with a bottom of " << bott << "and a top of " << top << "is " << area3;
     }

}

void handle_ellipse()
{
     cout<<"\nWhat is the length of the semi-major axis of the ellipse? ";
     cin>> majxis;
    
     if(majxis <=0)
     {
         cout<<"Invalid value for the semi-major axis, try again: ";
     }
     else
     {
         cout<<"What is the length for the semi-minor axis of the ellipse? ";
     cin>> minxis;
     area4= PI*minxis*majxis;
     }
     if(minxis <=0)
     {
         cout<<"Invalid value for the semi-minor axis, try again: ";
     }
     else
     {
         cout<<"The area of an ellipse with a semi-major axis of " << majxis<< "and a semi-minor axis of " << minxis << "is " << area4;
     }

}

void handle_triangle()
{
    cout<<"\nWhat is the length of the base of the triangle?";
    cin>> trib;
    
    if(trib<=0)
    {
        cout<<"\nInvalid Base of triangle, try again:";
    }
    else
    {
        cout<<"\nWhat is the height of the triangle? ";
    cin>> trih;
    area2 = (0.5*trib)*trih;
    }
    if(trih<=0)
    {
        cout<<"\nInvalid height of triangle, try again:";
    }
    else
    {
        cout<<"\nThe area of a trangle with a base of " << trib << "and a height of "<< trih <<" is " << area2;
    }

}

void handle_circle()
{
    cout<<"\nWhat is the radius of the circle?";
    cin>> R1;
    area1 = PI*(R1 *R1);
    
    if( R1 <=0)
    {cout<<"\nRadius must be greater than 0, try again";
        cin>> R1;
    }
    else
    {
        cout<< "\nThe area of a circle with a radius of " << R1 << " is" << area1;
    }

}

您也可以使用 switch 语句,因为它比 if 语句更快

#include <iostream>
#include <iomanip>
using namespace std;

    float area1, area2, area3, area4;
    int R1;
    int trib;
    int trih;
    int top;
    int bott;
    int traph;
    int majxis;
    int minxis;
    const float PI= 3.14159;

void handle_circle();
void handle_ellipse();
void handle_triangle();
void handle_trapizoid();

int main()
{
    int number;
    cout<< "I'm starting my program ";
    cout<<"\n1.Circle";
    cout<<"\n2.triangle";
    cout<<"\n3.trapezoid";
    cout<<"\n4.ellipse ";
    cout<<"\nEnter the corresponding number of your choice(1-4):";
    cin>> number;
    
    switch(number)
    {
        case 1:
        handle_circle();
        break;
        
        case 2:
        handle_triangle();
        break;
        
        case 3:
        handle_ellipse();
        break;
        
        case 4:
        handle_trapizoid();
        break;
        
        default:
        {
        cout<<"Integer is invalid, try again ";
        }
    }
}

void handle_trapizoid()
{
         cout<<"\nWhat is the length of the top of the trapezoid? ";
     cin>> top;
    
    
     if(top <=0 )
     {
         cout<<"\n Invalid value for top length of trapezoid, try again";
     }
     else
     {
         cout<<"\nWhat is the length of the bottom of the trapezoid? ";
     cin>> bott;
     }
    
    if (bott <= 0)
     {
         cout<<"Invalid value for bottom length of trapezoid, try again: ";
     }
     else
     {
         cout<<"What is the height of the trapezoid? ";
     cin>>traph;
     area3 = (top+bott)*0.5+traph;
     }
     if (traph <= 0)
     {
         cout<<"Invalid value for height of trapezoid";
     }
     else
     {
         cout<<"The area of a trapezoid with a bottom of " << bott << "and a top of " << top << "is " << area3;
     }

}

void handle_ellipse()
{
     cout<<"\nWhat is the length of the semi-major axis of the ellipse? ";
     cin>> majxis;
    
     if(majxis <=0)
     {
         cout<<"Invalid value for the semi-major axis, try again: ";
     }
     else
     {
         cout<<"What is the length for the semi-minor axis of the ellipse? ";
     cin>> minxis;
     area4= PI*minxis*majxis;
     }
     if(minxis <=0)
     {
         cout<<"Invalid value for the semi-minor axis, try again: ";
     }
     else
     {
         cout<<"The area of an ellipse with a semi-major axis of " << majxis<< "and a semi-minor axis of " << minxis << "is " << area4;
     }

}

void handle_triangle()
{
    cout<<"\nWhat is the length of the base of the triangle?";
    cin>> trib;
    
    if(trib<=0)
    {
        cout<<"\nInvalid Base of triangle, try again:";
    }
    else
    {
        cout<<"\nWhat is the height of the triangle? ";
    cin>> trih;
    area2 = (0.5*trib)*trih;
    }
    if(trih<=0)
    {
        cout<<"\nInvalid height of triangle, try again:";
    }
    else
    {
        cout<<"\nThe area of a trangle with a base of " << trib << "and a height of "<< trih <<" is " << area2;
    }

}

void handle_circle()
{
    cout<<"\nWhat is the radius of the circle?";
    cin>> R1;
    area1 = PI*(R1 *R1);
    
    if( R1 <=0)
    {cout<<"\nRadius must be greater than 0, try again";
        cin>> R1;
    }
    else
    {
        cout<< "\nThe area of a circle with a radius of " << R1 << " is" << area1;
    }

}