试图在C中使用REPEAT UNTIL(DO ... WHILE)循环

时间:2017-10-26 15:58:10

标签: 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循环的条件有什么问题,请帮忙!

1 个答案:

答案 0 :(得分:1)

最简单的方法可能是:

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