如何停止无限循环?

发布于 2024-09-19 06:38:31 字数 2180 浏览 1 评论 0原文

我正在编写一个程序,该程序将计算三角形或正方形的面积,然后提示用户是否希望计算另一个。我的代码已经运行到可以计算任一形状的面积的程度,但随后不再继续执行代码的其余部分。例如,如果选择了正方形,则计算面积,然后返回到正方形边长的提示。我假设这又是 while 永远循环,但不知道如何阻止循环无限进行。

这是我的代码:

 #include<stdio.h>
 #include<math.h>
 
 int main(void)
 
 {
     float sq_side, tri_base, tri_height, Area;
     char shape, cont = 'Y';
     
     printf("Please select the type of shape you desire to calculate the area for:\n");
     printf("                                                                     \n");
     printf("   Square = S                             Triangle = T               \n");
     printf("   -------                                    x                      \n");
     printf("   :     :                                   x x                     \n");
     printf("   :     :                                  x   x                    \n");
     printf("   -------                                 xxxxxxx                   \n");
     printf("                                                                     \n");
     printf("Please select either S or T:");
     scanf("%c", &shape);
     while (cont != 'n' && cont != 'N')
     
        if (shape == 'S' || shape == 's')
        {
            printf("What is the length of the sides of the square?:\n");
            scanf("%f", &sq_side);
         
            Area = pow(sq_side,2);
         
            printf("The area of the square is %.2f.\n", Area);
        }   
         
        else if (shape == 'T' || shape == 't')
        {
            printf("What is the length of the base of the triangle?:\n");
            scanf("%f", &tri_base);
            printf("What is the height of the triangle?:\n");
            scanf("%f", &tri_height);
            
            Area = 0.5 * tri_base * tri_height;
            
            printf("The area of the triangle is %.2f.\n", Area);
        }   
        
        else 
        {
        printf("Error:  You have select an incorrect option.");
        }
        
        printf("Do you wish to calculate a new shape?");
        fflush(stdin);
        scanf("%c", &cont);
        
     return(0);
     
 }
 

I am putting together a program that will calculate the area of either a triangle or a square and then prompt the user whether they wish to calculate another. I've got the code working to the point that it will calculate the area of either shape, but then doesn't continue with the rest of the code. For example is square is selected, the area is calculated and then goes back to the prompt for the square's side. I am assuming that it is again the while looping forever, but don't know how to stop the loop from going on endlessly.

Here is my code:

 #include<stdio.h>
 #include<math.h>
 
 int main(void)
 
 {
     float sq_side, tri_base, tri_height, Area;
     char shape, cont = 'Y';
     
     printf("Please select the type of shape you desire to calculate the area for:\n");
     printf("                                                                     \n");
     printf("   Square = S                             Triangle = T               \n");
     printf("   -------                                    x                      \n");
     printf("   :     :                                   x x                     \n");
     printf("   :     :                                  x   x                    \n");
     printf("   -------                                 xxxxxxx                   \n");
     printf("                                                                     \n");
     printf("Please select either S or T:");
     scanf("%c", &shape);
     while (cont != 'n' && cont != 'N')
     
        if (shape == 'S' || shape == 's')
        {
            printf("What is the length of the sides of the square?:\n");
            scanf("%f", &sq_side);
         
            Area = pow(sq_side,2);
         
            printf("The area of the square is %.2f.\n", Area);
        }   
         
        else if (shape == 'T' || shape == 't')
        {
            printf("What is the length of the base of the triangle?:\n");
            scanf("%f", &tri_base);
            printf("What is the height of the triangle?:\n");
            scanf("%f", &tri_height);
            
            Area = 0.5 * tri_base * tri_height;
            
            printf("The area of the triangle is %.2f.\n", Area);
        }   
        
        else 
        {
        printf("Error:  You have select an incorrect option.");
        }
        
        printf("Do you wish to calculate a new shape?");
        fflush(stdin);
        scanf("%c", &cont);
        
     return(0);
     
 }
 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦里泪两行 2024-09-26 06:38:31

你缺少大括号。结果是只有 if 语句(包括 else if 链)实际上位于循环体中。 printf(及更高版本)不是此复合语句的一部分。

 while (cont != 'n' && cont != 'N')
 {
    if (shape == 'S' || shape == 's')
    {
        printf("What is the length of the sides of the square?:\n");
        scanf("%f", &sq_side);

        Area = pow(sq_side,2);

        printf("The area of the square is %.2f.\n", Area);
    }   

    else if (shape == 'T' || shape == 't')
    {
        printf("What is the length of the base of the triangle?:\n");
        scanf("%f", &tri_base);
        printf("What is the height of the triangle?:\n");
        scanf("%f", &tri_height);

        Area = 0.5 * tri_base * tri_height;

        printf("The area of the triangle is %.2f.\n", Area);
    }   

    else 
    {
    printf("Error:  You have select an incorrect option.");
    }

    printf("Do you wish to calculate a new shape?");
    fflush(stdin);
    scanf("%c", &cont);
}   

You're missing curly braces. The result was that only the if statement (which includes the chain of else ifs) was actually in the loop body. The printf (and later) is not part of this compound statement.

 while (cont != 'n' && cont != 'N')
 {
    if (shape == 'S' || shape == 's')
    {
        printf("What is the length of the sides of the square?:\n");
        scanf("%f", &sq_side);

        Area = pow(sq_side,2);

        printf("The area of the square is %.2f.\n", Area);
    }   

    else if (shape == 'T' || shape == 't')
    {
        printf("What is the length of the base of the triangle?:\n");
        scanf("%f", &tri_base);
        printf("What is the height of the triangle?:\n");
        scanf("%f", &tri_height);

        Area = 0.5 * tri_base * tri_height;

        printf("The area of the triangle is %.2f.\n", Area);
    }   

    else 
    {
    printf("Error:  You have select an incorrect option.");
    }

    printf("Do you wish to calculate a new shape?");
    fflush(stdin);
    scanf("%c", &cont);
}   
一直在等你来 2024-09-26 06:38:31

while 循环没有括号。因此 if elseif else 块之外的代码不会被调用。

目前,您的代码将转换为

while (cont != 'n' && cont != 'N')
{
    if (shape == 'S' || shape == 's')
    {}
    else if (shape == 'T' || shape == 't')
    {}   
    else 
    {}
}

printf("Do you wish to calculate a new shape?");
fflush(stdin);
scanf("%c", &cont);

您想要的时间

while (cont != 'n' && cont != 'N')
{
    if (shape == 'S' || shape == 's')
    {}
    else if (shape == 'T' || shape == 't')
    {}   
    else 
    {}

    printf("Do you wish to calculate a new shape?");
    fflush(stdin);
    scanf("%c", &cont);
}

请记住,如果您在控制结构中不包含花括号,它只会调用下一条语句,在您的情况下,它是一系列嵌套的 if-else if 语句。

希望有帮助 - 瓦尔

Theres no brackets for you while loop. So the code outside of your if elseif else block is not being called.

currently your code translates to

while (cont != 'n' && cont != 'N')
{
    if (shape == 'S' || shape == 's')
    {}
    else if (shape == 'T' || shape == 't')
    {}   
    else 
    {}
}

printf("Do you wish to calculate a new shape?");
fflush(stdin);
scanf("%c", &cont);

when you want

while (cont != 'n' && cont != 'N')
{
    if (shape == 'S' || shape == 's')
    {}
    else if (shape == 'T' || shape == 't')
    {}   
    else 
    {}

    printf("Do you wish to calculate a new shape?");
    fflush(stdin);
    scanf("%c", &cont);
}

Remember that if you don't include curly braces in a control structure, it only calls the next statement, which in your case is a series of nested if-else if statements.

Hope it helps - Val

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文