简体   繁体   English

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

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

So I am writing this code for simple shape determining program, and I was trying to loop it, the condition, for the loop to end is when the value of the variable x is equal to 0 (all of teh variables are angels), since an angle cannot be 0, I think it is only logical to have the loop loop until the value entered is 0. However, the code does not seem to stop iterating even after the condition is met.所以我正在为简单的形状确定程序编写这段代码,我试图循环它,循环结束的条件是变量 x 的值等于 0(所有变量都是天使),因为角度不能为 0,我认为循环循环直到输入的值为 0 才合乎逻辑。但是,即使满足条件,代码似乎也不会停止迭代。 the code is below:代码如下:

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

I cannot really see what the problem with the condition for the while loop is, please help!我真的看不出 while 循环的条件有什么问题,请帮忙!

The easiest way may be:最简单的方法可能是:

    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