0

可能重复:
退出带有负整数的while循环

我希望用户输入几个不同的数字,然后分别测试每个数字。

此外,我只需要在输入数字时终止程序0

这是我的代码的开头:

int userInput;
printf("please enter some numbers: \n");
while ((scanf("%d", &userInput)) == 1)
{
    ...
}

我怎样才能不断地从用户那里得到输入,直到0被输入?

4

2 回答 2

5
while ((scanf("%d", &userInput) == 1) && (userInput != 0))
{
   ...
}
于 2013-01-28T22:40:29.643 回答
4
while (scanf("%d", &userInput))
{
    if(userInput == 0)
        break;
    /* do sth */
}
于 2013-01-28T22:39:33.750 回答