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