我是C新手,他有一個基本程序,要求用戶輸入一個數字,然後打印這個數字以及它在1-49的範圍。如何用0結束循環?
-5
A
回答
1
#include <stdio.h>
int main() {
for (;;) {
printf("Enter a number: ");
char buf[10];
fgets(buf, 10, stdin);
printf("You entered %s\n", buf);
// Code for displaying the range
if (buf[0] == '0')
break;
}
printf("Outside the loop\n");
return 0;
}
+4
討厭迂腐,但是如果你輸入一個像038這樣的0填充的數字,這將失敗。 – faviouz
3
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char **argv)
{
int n;
do {
// Read the number into n
printf("Enter a number: ");
if (scanf("%d", &n) != 1) {
perror("scanf");
exit(EXIT_FAILURE);
}
// Check arbitrary condition
if (n >= 1 && n <= 49) {
printf("%d is in the range 1-49\n", n);
}
} while (n != 0);
return EXIT_SUCCESS;
}
相關問題
- 1. 爲什麼選擇0會在循環結束時結束?
- 2. 循環一個代碼並按0結束循環
- 3. 如何使用串結束循環
- 4. 結束while循環
- 5. 如何結束這個循環
- 6. 如何正確結束while循環?
- 7. 我如何結束這個循環
- 8. 如何在Java中結束while循環
- 9. javascript for循環不會循環結束
- 10. For循環不循環,確認結束
- 11. 如何結束while循環並跳過for循環?
- 12. 循環未結束。我如何擺脫循環?
- 13. EOF和GETCHAR結束循環
- 14. 休息;不結束循環
- 15. 退出循環結束
- 16. EXCEPTION在循環結束
- 17. 循環結束,但不是
- 18. Haskell結束循環返回
- 19. 雖然循環不結束
- 20. 按'q'結束for循環
- 21. C#while循環不結束
- 22. 循環內循環 - 在autohotkey中檢測循環結束
- 23. 如何在輸入0時結束(雙數據類型)輸入循環?
- 24. 用戶輸入在C++結束循環
- 25. 負數結束程序使用循環?
- 26. 如何使用用戶輸入啓動和結束循環?
- 27. jQuery:如何在循環函數結束後自動調用?
- 28. 如何使用bool sentinel值在C++中結束while循環?
- 29. 如何調用已經結束的循環?
- 30. 光標循環;如何在循環開始/結束時只執行一次?
如果你發佈你的代碼它會更清楚。另外,什麼範圍?如果我輸入'7',那麼你的程序應該做什麼? – asimes
如果您發佈了7,它將打印7並打印另一個語句以顯示它在1-49範圍內。 (如果用戶輸入較大的數字,他們會得到一個聲明,並且程序要求輸入不同的數字。) –
將您的代碼用於獲取其編號並在無限循環內響應消息。當他們進入'0'時打破循環或退出程序 – asimes