#include <time.h>
#include <stdio.h>
#include <stdlib.h>
int rollDice(void);
int singleTurn(void);
int main(int argc, const char * argv []) {
int result,
userInput,
points = 100;
while(points > 0) {
printf("你现在剩余的钱数为: %d\n", points);
// 用户输入压注
printf("请输入您的下注数目: ");
scanf("%d", &userInput);
getchar();
// 判断用户下注数目
if (userInput > points)
printf("你没有这么多钱\n\n");
else {
result = singleTurn();
// 输出结果
if (result) {
points += userInput;
printf("你赢了\n\n");
} else {
points -= userInput;
printf("你输了\n\n");
}
}
}
return 0;
}
// 掷骰子
int rollDice(void) {
int x;
// 生成骰子点数总和
srand((unsigned)time(NULL));
x = rand()%12 + 1;
return x;
}
// 进行单轮回合
int singleTurn(void) {
int x, std;
printf("点击Enter键以掷出骰子...");
getchar();
x = rollDice();
printf("你掷出的骰子点数为: %d\n\n", x);
// 胜利
if (x == 7 || x == 11) return 1;
// 失败
else if (x == 2 || x == 3 || x == 12) return 0;
// 其他情况
else {
std = x;
do {
printf("点击Enter键以掷出骰子...");
getchar();
x = rollDice();
printf("你掷出的骰子点数为: %d\n\n", x);
// 失败
if (x == 7) return 0;
// 胜利
if (x == std) return 1;
} while (x != std);
}
exit(-1);
}
给你注释上了:
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
int rollDice(void);//制骰子
int singleTurn(void);
int main(int argc, const char * argv []) {//主函数
int result,
userInput,
points = 100;
while(points > 0) {//如果还有钱的话下注
printf("你现在剩余的钱数为: %d\n", points);
printf("请输入您的下注数目: ");// 用户输入压注
scanf("%d", &userInput);
getchar();
if (userInput > points)// 判断用户下注数目,是否小于拥有的钱
printf("你没有这么多钱\n\n");
else {
result = singleTurn();//有钱,判断输赢
if (result) {
points += userInput;//result>0赢,<0输 更新拥有的钱数量
printf("你赢了\n\n");
} else {
points -= userInput;
printf("你输了\n\n");
}
}
}
return 0;
}
// 掷骰子
int rollDice(void) {
int x;
srand((unsigned)time(NULL));// 利用随机函数srand生成骰子点数总和
x = rand()%12 + 1;
return x;
}
// 进行单轮回合
int singleTurn(void) {
int x, std;
printf("点击Enter键以掷出骰子...");
getchar();
x = rollDice();// 调用掷骰子函数
printf("你掷出的骰子点数为: %d\n\n", x);
if (x == 7 || x == 11) return 1;// 胜利
else if (x == 2 || x == 3 || x == 12) return 0;// 失败
else {// 其他情况
std = x;
do {
printf("点击Enter键以掷出骰子...");
getchar();
x = rollDice();// 调用掷骰子函数
printf("你掷出的骰子点数为: %d\n\n", x);
if (x == 7) return 0;// 失败
if (x == std) return 1;// 胜利
} while (x != std);
}
exit(-1);
}
您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632