我有一个条件,但是当条件不满足时程序仍会打印

问题描述 投票:0回答:1

想要该程序只是结束而不打印任何内容,试图在while循环中包括printf,但是它没有解决我的问题。

#include <stdio.h>
#include <limits.h>

int main()
{
    int i, max;
    i = 0;
    max = INT_MIN;

    while (i >-1) {
        if (i > max) {
            max = i;
        }
        printf("give a number: ");
        scanf("%d", &i);
    }
    printf("max= %d\n", max);
    return 0;
}

当我输入-2或任何负数为max = 0并且程序结束时的结果。

c
1个回答
0
投票

确定,因此,如果您要一直打印'running'最大值,直到或除非用户输入-ve数字,否则请尝试以下操作:

#include <stdio.h>
#include <stdio.h>
#include <limits.h>

int main()
{
    int i, max;
    i = 0;
    max = INT_MIN;

    while (i > -1) {
        printf("give a number: "); // Ask for number ...
        scanf("%d", &i);           // ... and fetch it before doing anything else!
        if (i >= 0) { // Only process non-negatives ...
            if (i > max) max = i;
            printf("max= %d\n", max); // Show running max value
        }
    }
    return 0;
}

随时要求进一步的解释和/或澄清。

© www.soinside.com 2019 - 2024. All rights reserved.