為什么只輸出了1 2
namespace ConsoleApplication1
{
? ? class Program
? ? {
? ? ? ? static void Main(string[] args)
? ? ? ? {
? ? ? ? ? ? int x = 1;?
? ? ? ? ? ? while(x < 10)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? if(x==3||x==8)
? ? ? ? ? ? ? ? ? continue;//請添加代碼,過濾3和8
? ? ? ? ? ? ? ? Console.Write(x);
? ? ? ? ? ? ? ? ?x++;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ??
? ? }
}
2018-05-18
當x=3時,因為if語句判斷,執行了continue語句,跳過了后面的x++,直接進行下一次循環。然而,x因為沒有進行x++的操作,會一直卡在x=3然后continue繼續循環的死循環中。解決辦法:把x++;挪到if語句上面!
2018-07-20
當執行if語句的時候變量自加跳出執行下次循環,如果不加上x++會同樣陷入死循環。