又係c loop既問題

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>

int main(void)
{
    int num;
    char anyChar ='a';
   
    for(num = 0; num <5; num++)
    {
            printf("\nType in a letter form 'a' to 'e':\n");
            
            while ((anyChar=getche()) !='d')
            {
                  printf("\nSorry , %c is incorrect.\n", anyChar);
                  printf("Try again.\n");
                  }
                  printf("\nThat is it!\n");
                  }
                  printf("Game is over!\n");
                  system("pause");
                  }
                  


點先可以限制輸入5次,5次後一定要出game is over(不論對錯)

原帖由 cghncghn 於 2008-11-11 19:25 發表
int main(void)
{
    int num;
    char anyChar ='a';
   
    for(num = 0; num <5; num++)
    {
            printf("\nType in a letter form 'a' to 'e':\n");
            
            while ((anyChar=getche()) !='d')
            {
                  printf("\nSorry , %c is incorrect.\n", anyChar);
                  printf("Try again.\n");
            }
            printf("\nThat is it!\n");
    }
    printf("Game is over!\n");
    system("pause");
}

你唔應該用for loop,唔係ge話,照你e+個program,要搭o岩5次要出到printf("Game is over!\n");呢句……
你應該係個while到加多個condition「num<=5」(入while loop前一開始set num做1),while入要就「num++」……

PS 你個program D括號最好對齊番位,唔係會好難睇……

TOP

睇下岩唔岩用
  1. int main(void)
  2. {
  3.         int count = 0;
  4.         char anyChar ='a';
  5.    
  6.         printf("\nType in a letter form 'a' to 'e':\n");
  7.         while (anyChar=getche() || ++count <5)
  8.         {
  9.                 if(anyChar != 'd')
  10.                         printf("\nSorry , %c is incorrect.\nTry again.\n", anyChar);
  11.                 else
  12.                 {
  13.                         printf("\nThat is it!\n");
  14.                         break;
  15.                 }
  16.         }
  17.         printf("Game is over!\n");
  18.         system("pause");
  19. }
複製代碼

[ 本帖最後由 梁炳 於 2008-11-12 00:35 編輯 ]

TOP

原帖由 梁炳 於 2008-11-12 00:33 發表
睇下岩唔岩用int main(void)
{
        int count = 0;
        char anyChar ='a';
   
        printf("\nType in a letter form 'a' to 'e':\n");
        while (anyChar=getche() || ++count  

唔知點解唔得 打任何字都係再打過

TOP

原帖由 TKY 於 2008-11-12 00:19 發表

你唔應該用for loop,唔係ge話,照你e+個program,要搭o岩5次要出到printf("Game is over!\n");呢句……
你應該係個while到加多個condition「num



姐係張個NUM++咁樣加入去??

while ((anyChar=getche()) !='d', num<5, num++)

TOP

  1. int main(void)
  2. {
  3.         int count = 0;
  4.         char anyChar ='a';
  5.    
  6.         printf("\nType in a letter form 'a' to 'e':\n");
  7.         while (count <5)
  8.         {
  9.                 if((anyChar=getch())  !=  'd')
  10.                     {
  11.                         printf("\nSorry , %c is incorrect.\nTry again.\n", anyChar);
  12.                         count++;
  13.                     }
  14.                 else
  15.                     {
  16.                         printf("\nThat is it!\n");
  17.                         break;
  18.                     }
  19.         }
  20.         printf("Game is over!\n");
  21.         system("pause");
  22. }
複製代碼
唔知得唔得.....

TOP

原帖由 cghncghn 於 2008-11-12 01:11 發表



姐係張個NUM++咁樣加入去??

while ((anyChar=getche()) !='d', num

當然唔係啦
while點會係咁樣寫……你學過while,都知while唔可以咁寫ga la……
你自己試試都知啦,個complier一定會話你while果行有野錯……
大約類似下面ge咁樣啦,你自己再執執……
  1. int main(void)
  2. {
  3.     int num;
  4.     char anyChar ='a';

  5.     printf("\nType in a letter form 'a' to 'e':\n");
  6.     num=1;
  7.    
  8.     while ((anyChar=getche()) !='d' && num<=1)
  9.     {
  10.                   printf("\nSorry , %c is incorrect.\n", anyChar);
  11.                   printf("Try again.\n");
  12.                   num++;
  13.     }

  14.     printf("\nThat is it!\n");
  15.     printf("Game is over!\n");
  16.     system("pause");
  17. }
複製代碼
另外,我唔建議你跟樓上用break去做……唔係ge話你個program ge 可讀性會大大減低……

TOP