你试着用while(scanf("%d%d",&x,&y)!=EOF)这个循环来做下,其中那个scanf返回的是输入的个数,EOF在标准库下是-1
例: while(scanf("%d%d",&x,&y)!=EOF&&(x!=0||y!=0)){ //自己补充.......}
#include <stdio.h> #define MAX_LINES 100 // 最大處理100行 unsigned int GetLines(const int UpLimit); void ClearInputBuffer(); void Pause(void); void main(void) { unsigned int Lines = GetLines(MAX_LINES); printf_s("您輸入的是: %d\n\n", Lines); if (Lines == 0) { Lines = MAX_LINES; } int x, y; unsigned int Counter = 0; do { x = 0; y = 0; printf_s("請輸入第%d行數據(x,y): ", Counter + 1); scanf_s("%d,%d", &x, &y); printf_s("x=%d, y=%d\n", x, y); // 要集中輸出,把數據轉存到數組、鏈表或集合中,自己喜歡怎樣處理都沒問題 ClearInputBuffer(); } while ((++Counter < Lines) && !((x == 0) && (y == 0))); Pause(); } unsigned int GetLines(const int UpLimit) { unsigned int Lines = 0; do { printf_s("請輸入欲讀入的數據行數(0-%d): ", MAX_LINES); if (scanf_s("%u", &Lines) < 1) { Lines = 0; } ClearInputBuffer(); } while(Lines > UpLimit); return Lines; } // 清空輸入緩衝區 void ClearInputBuffer(void) { while (getchar() != '\n') { ; } } // 暫停 void Pause(void) { int ch; do { ch = getchar(); } while((ch != EOF) && (ch != '\n')); }