检查第二个数字是否大于或小于第一个数字

时间:2016-10-04 18:09:23

标签: c conditional

我在做一个测试程序。将有2个输入,第一个数字和第二个数字。如果第一个数字大于或小于第一个数字,如果第一个数字小于第二个数字,则检查第一个数字。用户将再次输入“输入第二个数字”,我不知道在那里我应该怎么做。感谢

这是我的代码:

#include<stdio.h>

int main()
{
    int number1,number2,total;
    printf("Enter first number");
    scanf("%d",&number1);
    printf("Enter second number");
    scanf("%d",&number2);
    if(number1 > number2)
    {
      total = number1 - number2;
      printf("%d",total);        
    }
    else
    {
        printf("Number 1 is less than to number 2");
    }

    return 0;

}

1 个答案:

答案 0 :(得分:0)

#include <stdio.h>

int main(void){
    int number1, number2, total;

    printf("Enter first number >");
    scanf("%d", &number1);

    while(1){
        printf("Enter second number >");
        int status = scanf("%d", &number2);//check return value of scanf
        if(status == 1){// read integer but No check such as 3.5, 3?
            if(number1 < number2)
                printf("Number 1 is less than to number 2\n");
            else
                break;
        } else if(status == 0){//can't read integer
            printf("invalid input.\n");
            scanf("%*[^\n]");scanf("%*c");//clear up to newline
        } else {//if(status == EOF){//you want finish ;-)
            puts("bye");
            return -1;
        }
    }

    total = number1 - number2;
    printf("%d\n", total);      

    return 0;
}