Re: [問題] 請問骰子的問題

看板C_and_CPP作者 (vinci)時間14年前 (2011/04/03 16:28), 編輯推噓2(203)
留言5則, 3人參與, 最新討論串2/3 (看更多)
我重新修改了程式,使用getchar()來獲取觸發字元。 執行結果是可以成功觸發,但是當第二次以後的擲骰子動作 都會重複列印出Enter 'd' or to dice: 這個提示語。請問還有什麼要修改的嗎? #include <stdio.h> #include <stdlib.h> #include <time.h> enum Status {CONTINUE, WON, LOST}; int rollDice (void); int dice; int main () { int sum; int mypoint; enum Status gameStatus; /*gameStatus can contain CONTINUE, WOW, LOST*/ srand(time(NULL)); sum = rollDice(); switch (sum) { case 7: case 11: gameStatus = WON; break; case 2: case 3: case 12: gameStatus = LOST; break; default: gameStatus = CONTINUE; mypoint = sum; printf ("Your point is %d\n", mypoint); break; } while (gameStatus == CONTINUE) { sum = rollDice(); if (sum == mypoint) { gameStatus = WON; } else { if (sum == 7) { gameStatus = LOST; } } } if (gameStatus == WON) { printf("YOU WIN!!\n"); } else { printf("YOU LOST!!!\n"); } return 0; } int rollDice (void) { int dice1; int dice2; int dice_sum; printf("Enter 'd' or to dice:"); dice = getchar(); if (dice == 'd'){ dice1 = 1 + (rand() % 6); dice2 = 1 + (rand() % 6); dice_sum = dice1 + dice2; printf ("Your sum are %d + %d = %d\n", dice1, dice2, dice_sum); } return dice_sum; } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 211.76.74.83

04/03 16:38, , 1F
你可以把提示語從rolldice()拿出來
04/03 16:38, 1F

04/03 16:40, , 2F
這是換行字元問題
04/03 16:40, 2F

04/03 16:44, , 3F
我將提示拿出來了,並且在兩個調用rolldice()之前
04/03 16:44, 3F

04/03 16:45, , 4F
添加提示語,發現問題在while(gameStatus == CONTINUE)
04/03 16:45, 4F

04/03 16:45, , 5F
裡面的提示語,總是重複列印
04/03 16:45, 5F
文章代碼(AID): #1Dc2_0SR (C_and_CPP)
文章代碼(AID): #1Dc2_0SR (C_and_CPP)