對於我正在嘗試編寫的程序,我必須創建一個程序,要求用戶輸入一個數字並計算輸入的所有數字的總和,直到用戶輸入-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