| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 501 人关注过本帖
标题:根据C Primer书编的,为什么我的C语言中运行时骰子和始终为0?
只看楼主 加入收藏
天霁
Rank: 2
等 级:论坛游民
帖 子:33
专家分:18
注 册:2015-7-17
结帖率:83.33%
收藏
 问题点数:0 回复次数:1 
根据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);


图片附件: 游客没有浏览图片的权限,请 登录注册
搜索更多相关主题的帖子: C语言 
2015-10-01 16:51
天霁
Rank: 2
等 级:论坛游民
帖 子:33
专家分:18
注 册:2015-7-17
收藏
得分:0 
我已经找到问题了,rollem()返回值错误
2015-10-01 17:37
快速回复:根据C Primer书编的,为什么我的C语言中运行时骰子和始终为0?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018457 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved