![](/img/trans.png)
[英]Why does my code keep returning 1 with the error message even though I input a digit?
[英]Why is my code not giving any output even though there is no error in the code?
我是 C 的新手。 我必須用 0 初始化“a”,所需的 output 是使用 while 循環從 10 到 20 的自然數,但即使代碼中沒有錯誤,也沒有得到 output。
#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;
}
a
變量為零,這允許您進入循環。
不幸的是,它只有在它已經大於或等於 10 時才會增加,它永遠不會增加,因為它只有在它已經大於或等於 10 時才會增加,它永遠不會......等等,無窮無盡。
您可能應該將a++
移到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;
}
output 更接近你想要的:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.