我是 C 新手。我必須用 0 初始化 'a' ,使用 while 回圈所需的輸出是從 10 到 20 的自然數,但即使代碼中沒有錯誤也沒有輸出。
#include <stdio.h>
int main()
{
int a=0;
while (a<=20) {
if (a>=10) {
printf("The Value of A is %d\n",a);
a ;
}
}
return 0;
}
uj5u.com熱心網友回復:
該a變數是零,它允許你進入回圈。
不幸的是,只有當它已經大于或等于 10 時它才會增加,它永遠不會,因為它只在它已經大于或等于 10 時才增加,它永遠不會......等等,無窮無盡。
您可能應該將a to移動到陳述句的右大括號之后if。這樣,無論其當前值如何,它都會增加:
#include <stdio.h>
int main(void) {
int a = 0;
while (a <= 20) {
if (a >= 10) {
printf("The Value of A is %d\n", a);
// <- NOT HERE,
} //
a ; // <- BUT HERE.
}
return 0;
}
其輸出更接近您似乎想要的:
The Value of A is 10
The Value of A is 11
The Value of A is 12
The Value of A is 13
The Value of A is 14
The Value of A is 15
The Value of A is 16
The Value of A is 17
The Value of A is 18
The Value of A is 19
The Value of A is 20
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/379843.html
下一篇:如何將字串轉換為double?
