掷骰子游戏

您所在的位置:网站首页 掷骰子小游戏在线玩 掷骰子游戏

掷骰子游戏

2024-04-08 19:32| 来源: 网络整理| 查看: 265

    编写函数模拟掷骰子的游戏(两个骰子)。第一次掷的时候,如果点数之和为7或11则获胜;如果点数之和为2、3或12则落败;其他情况下的点数之和称为“目标”,游戏继续。在后续的投掷中,如果玩家再次掷出“目标”点数则获胜,掷出7则落败,其他情况都忽略,游戏继续进行。每局游戏结束时,程序询问用户是否再玩一次,如果用户输入的回答不是y或Y,程序会显示胜败的次数然后终止。

#include #include #include int main() { int winCount, lostCount; int first, second; int target; char input; winCount=lostCount=0; srand(time(0)); first=rand()%6+1; second=rand()%6+1; printf("%d %d\n",first,second); target=first+second; if(target==7 || target==11) { printf("you win\n"); winCount++; } else { if(target==2 || target==3 || target==12) { printf("you lost\n"); lostCount++; } else { printf("proceed or not?"); input=getchar(); while(input=='Y' || input=='y') { first=rand()%6+1; second=rand()%6+1; printf("%d %d\n",first,second); if(target==first+second){ printf("you win\n"); winCount++; } else if (first+second==7){ printf("you lost\n"); lostCount++; } printf("proceed or not?"); getchar(); input=getchar(); } } } printf("win=%d, lost=%d\n",winCount, lostCount); return 0; }



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3