2016-04-25 80 views
-1

我正在使用switch case爲我的布爾表達式與答案是或否。但是如果答案無效,我希望它能夠循環回來。我如何使用if或while循環與字符中的回答可以變成'y''Y''n''N'?下面的if語句可以工作嗎?C編程循環開關的情況下,如果它是默認的

if (mstatus != 'y', 'Y', 'N', 'n') 
{ 
    switch(mstatus) 
     case 'y': 
      printf("You have answer yes for married"); 
      break; 
     case 'Y': 
      printf("You have answer yes for married"); 
      break; 
     case 'n': 
      printf("You have answer no for married"); 
      break; 
     case 'N': 
      printf("You have answer no for married"); 
      break; 
} 
else 
{ 
    default: 
     printf("please re-enter a valid answer"); 
     scanf(" %c", &mstatus); 
} 
return 0; 
+0

請[編輯]這個問題,並縮進代碼正常。 – user694733

+2

一個案例(雙關語意圖)的貫通開關案例邏輯,如果有的話。 – WhozCraig

回答

1

switch語句的通用格式如下:

switch(expression) { 

    case constant-expression : 
     statement(s); 
     break; /* optional */ 

    case constant-expression : 
     statement(s); 
     break; 
     . 
     . 
     . 

    default : 
     statement(s); 
     break; 

這意味着default選擇將沒有一例是匹配的選擇。因此,您不需要在default聲明中包含該案例。你可以有一個看得更遠約this link.

如果你想,直到你匹配的情況下,一個連續讀取一個字符,包括switch循環,就像這樣:

int flag = 0; 
for (; flag == 1;) 
{ 
    switch(mstatus) 
    { 
     case 'y': 
     case 'Y': 
      printf("You have answer yes for married"); 
      flag = 1; 
      break; 
     case 'n': 
     case 'N': 
      printf("You have answer no for married"); 
      flag = 1;     
      break; 
     default: 
      printf("please re-enter a valid answer"); 
      scanf(" %c", &mstatus); 
    } 
}  
+0

非常感謝。 – DerCha

+0

@DerCha歡迎您:) – Marievi

1

根本就不需要if..else這個塊。

如果mstatus沒有任何一個y', 'Y', 'N', 'n',則控件將轉到default的情況。

你可以,但是,把switch語句塊中while(1)/while(someFlag)環和(聯合國)設定someFlag當你想跳出循環的

0

if語句不起作用,因爲您無法將變量與一系列值進行比較。此外,switch聲明可能是矯枉過正。如果你只是希望周圍循環,直到你得到一個正確的答案,那麼你可以做這樣的事情:

for(;;) 
{ 
    char mstatus; 
    printf("please enter a marriage status"); 
    scanf(" %c", &mstatus); 

    if(mstatus == 'y' || mstatus =='Y') 
    { 
    printf("You have answer yes for married"); 
    break; 
    } 
    else if(mstatus == 'n' || mstatus =='N') 
    { 
    printf("You have answer no for married"); 
    break; 
    } 
    else 
    { 
    printf("please re-enter a valid answer"); 
    } 
} 
0

在您所提供的代碼,您沒有添加一個循環的。你需要添加一個循環才能工作。

另外,如Saurav所述,不需要switch語句頂部的if,then else語句。

有很多方法可以做到這一點。一種方法是:

do 
{ 
    printf("Answer (y/n)? :"); 
    scanf(" %c", &mstatus); 
    repeat = 0; 
    switch(mstatus) 
    { 
    case 'y': 
    case 'Y': 
     printf("You have answer yes for married"); 
     break; 
    case 'n': 
    case 'N': 
     printf("You have answer no for married"); 
     break; 
    default: 
     printf("please re-enter a valid answer"); 
     repeat = 1; 
    } 
} while (repeat == 1); 
+0

非常感謝。 – DerCha

0

包裹邏輯的功能,這樣你可以從函數返回,當你想退出循環:

#include <stdio.h> 

int yesno(const char *q) 
{ 
    char answer = '\0'; 

    while (1) { 
     printf("%s? (y/n) ", q); 
     fflush(stdout); 

     scanf(" %c", &answer); 
     switch (answer) { 
     case 'N': 
     case 'n': 
      return 0; 
     case 'Y': 
     case 'y': 
      return 1; 

     default: 
      puts("Please answer y or n"); 
     } 
    } 

    /* unreachable */ 
    return -1; 
} 

int main(void) 
{ 
    int yes; 

    yes = yesno("Are you married"); 
    printf("You answered %s\n", yes ? "yes" : "no"); 
    return 0; 
} 
+0

謝謝,您的回答爲我提供了另一種編碼洞察。 – DerCha