C語言怎麼纔不會輸入錯誤的while一直循環

https://jingyan.baidu.com/article/b0b63dbf0e24c64a4930705a.html

https://zhidao.baidu.com/question/227556196.html

int a;      

printf("請輸入大於2的整數:");

/* 方式1 */
while(1){
   if(scanf("%d",&a) && a >= 2)
     break;
   puts("輸入錯誤!");
   while(getchar()!='\n');
   //fflush(stdin);
}
        
/* 方式2 */
while(scanf("%d",&a) && a < 2)
{
   puts("輸入錯誤!");
   while(getchar()!='\n');
}

 

發佈了88 篇原創文章 · 獲贊 123 · 訪問量 88萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章