|
|
20鱼币
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- int main(void)
- {
- int roll_count;
- int roll_n_dice(int dice, int sides);
- int dice,roll;
- int sides;
- srand((unsigned int) time(0)); 这里是不是与后面的rand想对应
- printf("Enter the number of sides per die, 0 to stop.\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 stop.\n");
- }
- printf("The rollem() function was called %d times.\n",
- roll_count);
- printf("GOOD FORTUNE TO YOU!\n");
- return 0;
- }
- int roll_count = 0;
- static 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;
- }
- 我输入6 2 编译对的 然后接着输入 6 a 调试的时候sides 值是6,但是为什么会结束了呢判断条件不是while (scanf("%d", &sides) == 1 && sides > 0)
- 请解释
复制代码
|
最佳答案
查看完整内容
一个骰子六面,共三个,模拟出三个投一次投出的总点数。用的随机函数。
|