如何停止无限循环?

编程入门 行业动态 更新时间:2024-10-21 13:40:23
本文介绍了如何停止无限循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在编写一个程序,该程序将计算三角形或正方形的面积,然后提示用户是否要计算另一个.我已经让代码工作到它会计算任一形状的面积,但不会继续执行其余的代码.例如选择正方形,计算面积,然后返回正方形边的提示.我假设这又是永远循环的 while 循环,但不知道如何阻止循环无休止地进行.

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.

这是我的代码:

 #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);
     
 }
 

推荐答案

您缺少花括号.结果是只有 if 语句(包括 else ifs 链)实际上在循环体中.printf(及更高版本)不是这个复合语句的一部分.

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);
}   

这篇关于如何停止无限循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-26 03:35:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1128588.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!