c语言switch中用ifelse,初学者求问:用if单分支选择语句替换switch语句?if.else呢...
菜鳥求問:用if單分支選擇語句替換switch語句?if...else呢?
主要是關(guān)于default情況的處理,代碼如下:
/*?Fig.?4.7:?fig04_07.c
Counting?letter?grades?*/
#include?
/*?function?main?begins?program?execution?*/
int?main(?void?)
{
int?grade;?/*?one?grade?*/
int?aCount?=?0;?/*?number?of?As?*/
int?bCount?=?0;?/*?number?of?Bs?*/
int?cCount?=?0;?/*?number?of?Cs?*/
int?dCount?=?0;?/*?number?of?Ds?*/
int?fCount?=?0;?/*?number?of?Fs?*/
printf(??"Enter?the?letter?grades.\n"??);
printf(??"Enter?the?EOF?character?to?end?input.\n"??);
/*?loop?until?user?types?end-of-file?key?sequence?*/
while?(?(?grade?=?getchar()?)?!=?EOF?)?{
/*?determine?which?grade?was?input?*/
switch?(?grade?)?{?/*?switch?nested?in?while?*/
case?'A':?/*?grade?was?uppercase?A?*/
case?'a':?/*?or?lowercase?a?*/
++aCount;?/*?increment?aCount?*/
break;?/*?necessary?to?exit?switch?*/
case?'B':?/*?grade?was?uppercase?B?*/
case?'b':?/*?or?lowercase?b?*/
++bCount;?/*?increment?bCount?*/
break;?/*?exit?switch?*/
case?'C':?/*?grade?was?uppercase?C?*/
case?'c':?/*?or?lowercase?c?*/
++cCount;?/*?increment?cCount?*/
總結(jié)
以上是生活随笔為你收集整理的c语言switch中用ifelse,初学者求问:用if单分支选择语句替换switch语句?if.else呢...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux中文系统换英文字体,linux
- 下一篇: c语言cin改scanf,我的代码用sc