While循环帮助 [英] While loop help

查看:214
本文介绍了While循环帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨...它的新手再次:)我组建了一个程序,将计算无论是三角形或正方形的区域,然后提示用户是否希望计算另一个。我已经得到了code一起,它将计算两种形状的面积来看,但不与code的其余部分继续。对于方是选择例如,面积计算,然后返回到提示符为方形的身边。我假设它再次永远循环的同时,但不知道如何停止无休止地从正在进行的循环。

我的继承人code:

 #包括LT&;&stdio.h中GT;
 #包括LT&;&MATH.H GT; INT主要(无效) {
     浮sq_side,tri_base,tri_height,面积;
     字符形状,续='Y';     的printf(请选择您希望计算区域形状的类型:\\ n);
     的printf(\\ n);
     的printf(方= S三角形= T \\ n);
     的printf(-------点¯x\\ n);
     的printf(:X X \\ n);
     的printf(:X X \\ n);
     的printf(------- XXXXXXX \\ n);
     的printf(\\ n);
     的printf(请选择S或T:);
     scanf函数(%C,&安培;形状);
     而(续='N'和;!&安培;!CONT ='N')        如果(形状=='S'形|| =='S')
        {
            的printf(什么是正方形的边长:\\ n吗?);
            scanf函数(%F,&安培; sq_side);            面积= POW(sq_side,2);            的printf(广场的面积为%.2f \\ n,区);
        }        否则,如果(形状=='T'形|| =='T')
        {
            的printf(什么是三角形的底边的长度:\\ N +);
            scanf函数(%F,&安培; tri_base);
            的printf(什么是三角形的高度:吗?\\ n);
            scanf函数(%F,&安培; tri_height);            面积= 0.5 * tri_base * tri_height;            的printf(三角形的面积为%.2f \\ n,区);
        }        其他
        {
        的printf(错误:你有选择不正确的选项);
        }        的printf(你要计算一个新的形状?);
        fflush(标准输入);
        scanf函数(%C,&安培;续);     返回(0); }


解决方案

您错过大括号内。其结果是,只有在if语句(包括其他IFS的链)实际上是在循环体。在的printf (及以后)是不是这个复合语句的组成部分。

 而(续='N'和;!&安培;!CONT ='N')
 {
    如果(形状=='S'形|| =='S')
    {
        的printf(什么是正方形的边长:\\ n吗?);
        scanf函数(%F,&安培; sq_side);        面积= POW(sq_side,2);        的printf(广场的面积为%.2f \\ n,区);
    }    否则,如果(形状=='T'形|| =='T')
    {
        的printf(什么是三角形的底边的长度:\\ N +);
        scanf函数(%F,&安培; tri_base);
        的printf(什么是三角形的高度:吗?\\ n);
        scanf函数(%F,&安培; tri_height);        面积= 0.5 * tri_base * tri_height;        的printf(三角形的面积为%.2f \\ n,区);
    }    其他
    {
    的printf(错误:你有选择不正确的选项);
    }    的printf(你要计算一个新的形状?);
    fflush(标准输入);
    scanf函数(%C,&安培;续);
}

Hey guys... its the newbie again :) I'm 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'm assuming that it's again the while looping forever, but don't know how to stop the loop from going on endlessly.

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

 }

解决方案

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆