相关疑难解决方法(0)

在rollDice函数内调用srand(time(NULL))时出现问题

当我在第一次使用 srand(time(NULL))rollDice()功能,它没有工作.但是当我把它放在主要部分时,它可以工作.这是为什么?你能告诉我逻辑吗?

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

int rollDice(void) {
    return (1+rand()%6) + (1+rand()%6);
}
int main(void) {
    int roll;
    srand(time(NULL));          
    roll = rollDice();
    printf("You rolled %d.\n", roll);

    enum Gamestatus {WON,LOST,CONTINUE};
    enum Gamestatus status;

    while(status==CONTINUE){
        printf("You are rolling again: \n");
        printf("You rolled %d\n", roll = rollDice());

        if (targetPoint==roll){
            printf("You win!");
            status=WON;
        }
        else if(7==roll){
            printf("You lost!");
            status=LOST;
        }
        else
            status=CONTINUE;
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c random srand

3
推荐指数
2
解决办法
9417
查看次数

标签 统计

c ×1

random ×1

srand ×1