#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
bool play_game(void); // 模拟游戏,进行一次游戏,返回一个bool值,决出胜负
int roll_dice(void); // 记录两个骰子之和
int roll_dice(void) {
int x, y, z;// 定义变量
x = rand() % 6 + 1;// 获取1到6之间的随机数
y = rand() % 6 + 1;
z = x + y;//两点数之和
printf("你的点数之后为 :%d\n", z);
return z;//返回Z
}
bool play_game(void) {
int p, t;
p = roll_dice();
if(p == 7 || p == 11) {
return true;
}
else if(p == 2 || p == 3 || p == 12) {
return false;
}
else {
t = p;
printf("你的点数之后为 :%d\n", t);
for(; ; ) {
p = roll_dice();
if(p == t) {
return true;
}
else if(p == 7) {
return false;
}
}
}
}
int main(int argc, const char * argv[]) {
// insert code here...
printf("骰子游戏\n");
bool b;
char ch = 'y';
int i = 100, j = 0;
srand((unsigned) time(0)); // 放在循环外面,更新种子,使得每次产生不同的随机数
do {
printf("请下注:\n");
scanf("%d",&j);
b = play_game();
if(b) {
printf("你赢了!\n\n");
i=i+j;
}
else {
printf("你输了!\n\n");
i=i-j;
}
printf("是否输入Y或者y继续游戏? ");
ch = getchar();
getchar(); // 除去回车符
printf("\n");
}
while(ch=='y') ; printf("您的钱数:%d",i);
return 0;
}
你每次输入下注后 回车被ch读入了,所以报错,这样就行了
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
bool play_game(void); // 模拟游戏,进行一次游戏,返回一个bool值,决出胜负
int roll_dice(void); // 记录两个骰子之和
int roll_dice(void)
{
int x, y, z;// 定义变量
x = rand() % 6 + 1;// 获取1到6之间的随机数
y = rand() % 6 + 1;
z = x + y;//两点数之和
printf("你的点数之后为 :%d\n", z);
return z;//返回Z
}
bool play_game(void)
{
int p, t;
p = roll_dice();
if(p == 7 || p == 11)
{
return true;
}
else if(p == 2 || p == 3 || p == 12)
{
return false;
}
else
{
t = p;
printf("你的点数之后为 :%d\n", t);
for(; ; )
{
p = roll_dice();
if(p == t)
{
return true;
}
else if(p == 7)
{
return false;
}
}
}
}
int main(int argc, const char * argv[])
{
// insert code here...
printf("骰子游戏\n");
bool b;
char ch = 'y';
int i = 100, j = 0;
srand((unsigned) time(0)); // 放在循环外面,更新种子,使得每次产生不同的随机数
do
{
printf("请下注:\n");
scanf("%d",&j);
b = play_game();
if(b)
{
printf("你赢了!\n\n");
i=i+j;
}
else
{
printf("你输了!\n\n");
i=i-j;
}
printf("是否输入Y或者y继续游戏? ");
getchar();
scanf("%c",&ch);
printf("\n");
}
while(ch=='y');
printf("您的钱数:%d",i);
return 0;
}
看错了
您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632