close
標題:

c# 判斷流程switch與case問題

發問:

商店開幕慶,購買金額裡面有一個數字7就打95折,兩個數字7就打8折,三個數字7就打6折,程式如下。int total; Console.Write("請輸入消費總額 "); total = int.Parse(Console.ReadLine()); int count = 0; if (total % 10 == 7) count++; if ((total / 10) % 10 == 7) ... 顯示更多 商店開幕慶,購買金額裡面有一個數字7就打95折,兩個數字7就打8折,三個數字7就打6折,程式如下。 int total; Console.Write("請輸入消費總額 "); total = int.Parse(Console.ReadLine()); int count = 0; if (total % 10 == 7) count++; if ((total / 10) % 10 == 7) count++; if ((total / 100) % 10 == 7) count++; switch (count) { case 1: Console.WriteLine("該筆消費將以95折計算"); Console.WriteLine("折扣後金額為: " + total * 0.95 + "元"); break; case 2: Console.WriteLine("該筆消費將以8折計算"); Console.WriteLine("折扣後金額為: " + total * 0.8 + "元"); break; case 3: Console.WriteLine("該筆消費將以6折計算"); Console.WriteLine("折扣後金額為: " + total * 0.6 + "元"); break; default: Console.WriteLine("該筆消費不符合折扣條件, 將以原價 " + total + "元計價"); break; } Console.Read(); 我不懂的是: 1.int count = 0; 為什麼指定為0? 2.count++; 為什麼還要加1 3.它要怎麼判別輸入的金額要用哪個case呢?(如果我要打6折就是case 3,可是也有可能跑到case 1?)

最佳解答:

解釋: count初始化為0和count++是用來判斷購買的金額屬於哪一種折扣的機制,在switch區塊裡面,可以看到若是count值為1,就是要打95折的情形,以此類推,count值為2是購買金額中有兩個數字7(對應到第二個if判斷式),count值3是最後一個情形(對應到第三個if判斷式)。 而這三個if判斷式是這樣的,它是從一個金額的最小位數開始判斷起,譬如說787這個金額,經過第一個if區塊,count值變為1,經過第二個if區塊,count值仍然為1(因為(787/10)%10=8),經過最後一個if判斷式,count值變為2,然後經過switch區塊,就會列印出打8折後的金額。 至於你的第三個問題,我猜你是要問會不會一個金額屬於第三種情形,但程式碼判斷成第一種,從你PO出來的程式碼來看,不會有這種問題,但我得說,這個程式碼只能判斷三位數的金額,比如說7477這個金額,經過三個if判斷式後,count值會是2而已。簡單來說,第一個if判斷式用來判斷個位數是否為7,第二個if判斷十位數是否為7,第三個if判斷式判斷百位數是否為7,而count只是用來累積7的次數的變數罷了。 總結: 1. 只是初始化而已,如果switch區塊裡面的case分別為4、5、6,則count一開始要指定為3。 2. 這是為了計算出7總共出現了幾次 3. 請看解釋

其他解答:

total = int.Parse(Console.ReadLine()); 這是一開始給使用者輸入一個數字整數的因為int 然後那個數字會存在 total變數裡面 int count = 0; //代表最原始的count是0 為什麼要+1 if (total % 10 == 7) 第1個判斷式 count++; if ((total / 10) % 10 == 7) 第2個判斷式 count++; if ((total / 100) % 10 == 7) 第3個判斷式 count++; 如果第1個判斷式成立 count =1 (0+1) 如果第2個判斷式也成立 count =2 (0+1+1) 又可以說是再+1 如果3個判斷都成立 count =3 (0+1+1+1) 如果只有其中一個成立 當然也只會+1 如果兩個+1+1 最後switch判斷coun這個變數 如果是幾就跳到case的幾 default是如果所有的case都不成立 break是case的結束點 請問這樣夠清楚嗎?|||||namespace discount { class Program { static int count7(string price) { int cnt = 0; foreach (char c in price) if ('7' == c) ++cnt; return cnt; } 2013-05-12 09:11:05 補充: static void Main(string[] args) { double[] discount = new double[] {100.0, 95.0, 80.0, 70.0}; Console.Write("Enter the total cost:"); string cost = Console.ReadLine(); int cn7 = count7(cost); 2013-05-12 09:11:48 補充: int price = int.Parse(cost); if (cn7 > 3) cn7 = 3; Console.WriteLine("the total discount is " + (100.0 - discount[cn7]) + "%"); Console.WriteLine("The price after discount is $" + (price * discount[cn7]/100.0)); 2013-05-12 09:12:07 補充: return; }}}|||||1. 如果沒有7時,計數器將是零。 2. 計數器將增加一個,如果有一個七。 3. 我不明白,似乎你不太明白你的要求。|||||你不能買超過 $999,不然就不會打折了。 例如我買了 $777000,就半折都不打。 應該把上面改成 int count = 0; while (total != 0) { if (total % 10 == 7) ++count; total /= 10; } 2013-05-10 23:21:49 補充: 你可以用筆算一遍,假設輸入 727 會發生什麼事。

aa.jpg

 

此文章來自奇摩知識+如有不便請留言告知

BFC66BE0445C3814
arrow
arrow

    ddhdxb5 發表在 痞客邦 留言(0) 人氣()