大家帮我看看这个掷骰子程序

u010179812 2013-05-11 11:15:09
下面是书中的一个程序,我照抄下来了,不知有没有写错,有些地方我没有明白,特来请教。
这个程序的目的是求点数总和,好像是这样,因为书中是用了三处来写这个程序,我把它们合并在一起,也不知对不对。

#include "stdio.h"
#include "stdlib.h"
#include "time.h"

int roll_count = 0;

int rollem(int sides);
int roll_n_dice(int dice, int sides);

int main(void)
{
int dice, roll;
int sides;

srand((unsigned int)time(0));
printf("Enter the number of sides per die, 0 to quit.\n");
while(scanf("%d", &sides)==1 && sides>0)
{
printf("how many dice?\n");
scanf("%d", &dice);
roll = roll_n_dice(dice, sides);
printf("You have rolled a %d using %d %d-sided dice.\n",
roll, dice, sides);
printf("How many sides? Enter 0 to quit.\n");
}
printf("The rollem() function was called %d times.\n", roll_count);
printf("GOOD FOUTURN TO YOU!\n");
return 0;
}

int rollem(int sides)
{
int roll;
roll = rand()%sides+1;
++roll_count;
return roll;
}

int roll_n_dice(int dice, int sides)
{
int d;
int total = 0;
if(sides < 2)
{
printf("Need at least 2 sides.\n");
return -2;
}
if(dice < 1)
{
printf("Need at least 1 die.\n");
return -1;
}
for(d=0;d<dice;d++)
{
total += rollem(sides);
}
return total;
}
我运行一次的结果是
Enter the number of sides per die, 0 to quit.
6
how many dice?
2
You have rolled a 5 using 2 6-sided dice.
How many sides? Enter 0 to quit.

以后每次输入6与2,得到的结果都不一样,是怎么回事?
...全文
151 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
u010179812 2013-05-12
  • 打赏
  • 举报
回复
谢谢楼上的各位。
折翼断JJ 2013-05-11
  • 打赏
  • 举报
回复
我这可以执行的,每次也不一样的。
u010179812 2013-05-11
  • 打赏
  • 举报
回复
怎么只出现一行呢?
u010179812 2013-05-11
  • 打赏
  • 举报
回复
没在成功发送。
u010179812 2013-05-11
  • 打赏
  • 举报
回复
哦,我试试,重发一次。
#include "stdio.h" #include "stdlib.h" #include "time.h" int roll_count = 0; int rollem(int sides); int roll_n_dice(int dice, int sides); int main(void) { int dice, roll; int sides; srand((unsigned int)time(0)); printf("Enter the number of sides per die, 0 to quit.\n"); while(scanf("%d", &sides)==1 && sides>0) { printf("how many dice?\n"); scanf("%d", &dice); roll = roll_n_dice(dice, sides); printf("You have rolled a %d using %d %d-sided dice.\n", roll, dice, sides); printf("How many sides? Enter 0 to quit.\n"); } printf("The rollem() function was called %d times.\n", roll_count); printf("GOOD FOUTURN TO YOU!\n"); return 0; } int rollem(int sides) { int roll; roll = rand()%sides+1; ++roll_count; return roll; } int roll_n_dice(int dice, int sides) { int d; int total = 0; if(sides < 2) { printf("Need at least 2 sides.\n"); return -2; } if(dice < 1) { printf("Need at least 1 die.\n"); return -1; } for(d=0;d<dice;d++) { total += rollem(sides); } return total; }
asdf20071203 2013-05-11
  • 打赏
  • 举报
回复
这个本来不是就要每次都不不一样么 。。。 要都一样,这个就 不 叫投掷骰子了吧
折翼断JJ 2013-05-11
  • 打赏
  • 举报
回复
看每个机器的不同的,还有编译器的,我这里就和你的不一样的。
折翼断JJ 2013-05-11
  • 打赏
  • 举报
回复

在下面找这个图标,点开选择语言就可以了。
u010179812 2013-05-11
  • 打赏
  • 举报
回复
为什么我发的程序都左对齐了?请网友告诉我怎样把那个带个行号的程序弄到这上面来,这样你们就更能直观地看这个程序。
hugett 2013-05-11
  • 打赏
  • 举报
回复

#include "stdio.h"
#include "stdlib.h"
#include "time.h"

int roll_count = 0;

int rollem(int sides);
int roll_n_dice(int dice, int sides);

int main(void)
{
	int dice, roll;
	int sides;

	srand((unsigned int)time(0));
	printf("Enter the number of sides per die, 0 to quit.\n");
	while(scanf("%d", &sides)==1 && sides>0)
	{
		printf("how many dice?\n");
		scanf("%d", &dice);
		roll = roll_n_dice(dice, sides);
		printf("You have rolled a %d using %d %d-sided dice.\n",roll, dice, sides);
		printf("How many sides? Enter 0 to quit.\n");
	}
	printf("The rollem() function was called %d times.\n", roll_count);
	printf("GOOD FOUTURN TO YOU!\n");
	return 0;
}

int rollem(int sides)
{
	int roll;
	roll = rand()%sides+1;//这里是用了随机的。。本来就是模拟掷骰子。。当然每次结果不一样。。你把这句改成roll = 1;保证每次结果都一样。。
	++roll_count;
	return roll;
}
 
int roll_n_dice(int dice, int sides)
{
	int d;
	int total = 0;
	if(sides < 2)
	{
		printf("Need at least 2 sides.\n");
		return -2;
	}
	if(dice < 1)
	{
		printf("Need at least 1 die.\n");
		return -1;
	}
	for(d=0;d<dice;d++)
	{
		total += rollem(sides);
	}
	return total;
}
u010179812 2013-05-11
  • 打赏
  • 举报
回复
You have rolled a %d using %d %d-sided dice 这句话翻译成中文是什么意思? You have rolled a 5 using 2 6-sided dice.谷歌翻译成“你已经推出了5 2 6面骰子”。难道是说“2个6面骰子这次的点数总和是5”?

70,035

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧