根据C Primer书编的,为什么我的C语言中运行时骰子和始终为0?
程序代码:
#include <stdio.h> #include <stdlib.h>//为srand()函数提供原型 #include <time.h>//为time()函数提供原型 #include "diceroll.h"//为roll_n_dice()和roll_count函数提供原型 int main(void) { int dice,roll; int sides; srand((unsigned int)time(0));//随机化种子 printf("Enter the number of sides per die,0 to stop.\n"); while(scanf("%d",&sides)==1&&sides>0) { printf("玩几次?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 stop.\n"); } printf("The rollem()function was called %d times.\n",roll_count); printf("GOOD FORTUNE TO YOU!\n"); return 0; }
程序代码:
//掷骰子的模拟程序 #include "diceroll.h" #include <stdio.h> #include <stdlib.h>//为rand()函数提供类库 int roll_count=0; static int rollem(int sides) { int roll; roll= rand()%sides +1; ++roll_count; return 0; } int roll_n_dice(int dice,int sides) { int d; int total=0; if(sides<2) { printf("至少需要使用2面骰子\n"); return -2; } if(dice<1) { printf("好歹玩一次呗!\n"); return -1; } for(d=0;d<dice;d++) total += rollem(sides); return total; }
extern int roll_count; int roll_n_dice(int dice,int sides);