Re: [問題] 請問骰子的問題
我重新修改了程式,使用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
04/03 16:38, 1F
推
04/03 16:40, , 2F
04/03 16:40, 2F
→
04/03 16:44, , 3F
04/03 16:44, 3F
→
04/03 16:45, , 4F
04/03 16:45, 4F
→
04/03 16:45, , 5F
04/03 16:45, 5F
討論串 (同標題文章)