对于我正在尝试编写的程序,我必须创建一个程序,要求用户输入一个数字并计算输入的所有数字的总和,直到用户输入-1来停止循环。但是,我无法打印-1或将其添加到总数中,但我正在为此付出努力。循环程序问题
#include <stdio.h>
int main()
{
int x, total;
total = 0;
x = 0;
while (x <= -2 || x >= 0)
{
printf("Please enter a number: ");
scanf("%d", &x);
printf("You entered %d \n", x);
totalSum = total + x;
printf("total is %d \n", total);
}
printf("Have a nice day :) \n");
printf("total is %d \n", total);
return 0;
}
任何建议,我怎么能停在-1循环没有它打印或加总?
哦,所以我需要添加条件来打破循环?谢谢你,分析它对我来说越来越清晰。 – user2092535 2013-03-15 04:09:00
@ user2092535正确的,当它应该:)时打破循环 – 2013-03-15 04:32:24