作者: cghncghn 時間: 2008-11-11 19:25 標題: 又係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(不論對錯)
作者: TKY 時間: 2008-11-12 00:19
原帖由 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括號最好對齊番位,唔係會好難睇……
作者: 梁炳 時間: 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 <5)
- {
- if(anyChar != 'd')
- printf("\nSorry , %c is incorrect.\nTry again.\n", anyChar);
- else
- {
- printf("\nThat is it!\n");
- break;
- }
- }
- printf("Game is over!\n");
- system("pause");
- }
[ 本帖最後由 梁炳 於 2008-11-12 00:35 編輯 ]
作者: cghncghn 時間: 2008-11-12 00:52
原帖由 梁炳 於 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
唔知點解唔得
作者: cghncghn 時間: 2008-11-12 01:11
原帖由 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++)
作者: Newbie 時間: 2008-11-12 09:49
- int main(void)
- {
- int count = 0;
- char anyChar ='a';
-
- printf("\nType in a letter form 'a' to 'e':\n");
- while (count <5)
- {
- if((anyChar=getch()) != 'd')
- {
- printf("\nSorry , %c is incorrect.\nTry again.\n", anyChar);
- count++;
- }
- else
- {
- printf("\nThat is it!\n");
- break;
- }
- }
- printf("Game is over!\n");
- system("pause");
- }
作者: TKY 時間: 2008-11-12 21:01
當然唔係啦
while點會係咁樣寫……你學過while,都知while唔可以咁寫ga la……
你自己試試都知啦,個complier一定會話你while果行有野錯……
大約類似下面ge咁樣啦,你自己再執執……
- int main(void)
- {
- int num;
- char anyChar ='a';
- printf("\nType in a letter form 'a' to 'e':\n");
- num=1;
-
- while ((anyChar=getche()) !='d' && num<=1)
- {
- printf("\nSorry , %c is incorrect.\n", anyChar);
- printf("Try again.\n");
- num++;
- }
- printf("\nThat is it!\n");
- printf("Game is over!\n");
- system("pause");
- }


