我只是希望它给我1到6之间的值,但它给了我这个:
P1d1 = 1445768086
P1d2 = -2
P2d1 = 1982468450
P2d2 = 198281572
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main (){
srand(time(NULL));
/*Player 1*/
int P1d1 = 1 + rand() % 6; //Rolls Player 1's first die; random # between 1-6
int P1d2 = 1+ rand() % 6; //Rolls Player 1's second die; random # between 1-6
int P1total = P1d1 + P1d2; //Takes total of both rolls
/*Player 2*/
int P2d1 = 1 + rand() % 6; //Rolls Player 2's first die; random # between 1-6
int P2d2 = 1 + rand() % 6; //Rolls Player 2's second die; random # between 1-6
int P2total = P2d1 + P2d2; //Takes total of both rolls
printf("P1d1 = %d\nP1d2 = %d\nP2d1 = %d\n P2d2 = %d\n");
}
Run Code Online (Sandbox Code Playgroud)
我不允许使用函数,因为我们还没有在课堂上介绍它们.很感谢任何形式的帮助!
您printf没有指定变量.因此,您将获得随机垃圾打印,而不是您正在寻找的实际变量值.
你应该这样:
printf("P1d1 = %d\nP1d2 = %d\nP2d1 = %d\n P2d2 = %d\n", P1d1, P1d2, P2d1, P2d2);
Run Code Online (Sandbox Code Playgroud)