繁体   English   中英

试图让 REPEAT UNTIL (DO...WHILE) 循环在 C 中工作

[英]Trying to get a REPEAT UNTIL (DO...WHILE) loop working in C

所以我正在为简单的形状确定程序编写这段代码,我试图循环它,循环结束的条件是变量 x 的值等于 0(所有变量都是天使),因为角度不能为 0,我认为循环循环直到输入的值为 0 才合乎逻辑。但是,即使满足条件,代码似乎也不会停止迭代。 代码如下:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int x=0;
    int y=0;
    int A=0;
    do {
    printf("What is the value of x?");
    scanf("%d", &x);
    printf("What is the value of y?");
    scanf("%d", &y);
    printf("What is the value of A?");
    scanf("%d", &A);
    if ((A==90)&&(x==y))
        {
            printf("Square\n");
        }
    else if ((A==90)&&(x!=y))
        {
            printf("Rectangle\n");
        }
     else if ((A==60)||(A==120))
        {
            printf("Hexagonal\n");
        }
     else if ((A!=60)||(A!=120)&&(x==y))
        {
            printf("Rhombic\n");
        }
     else if ((A!=60)||(A!=120)&&(x!=y))
        {
            printf("Parallelogram\n");
        }
    else
    {
        printf("The values you have entered are not supported by the program, 
try again!");

    }
    }while(x!=0);
    printf("Thanks for using the program!");



    system("pause");
    return 0;
}

我真的看不出 while 循环的条件有什么问题,请帮忙!

最简单的方法可能是:

    scanf("%d", &x);
    if (!x) break;        // break the loop, not asking anything else

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM