0

我只希望它给我 1 到 6 之间的值,但它给了我这个:

P1d1 = 1445768086

P1d2 = -2

P2d1 = 1982468450

P2d2 = 198281572

这是我的代码:

#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");

}

我不允许使用函数,因为我们还没有在课堂上讲过它们。很感谢任何形式的帮助!

4

1 回答 1

5

printf没有指定变量。因此,您打印的是随机垃圾,而不是您正在寻找的实际变量值。

你应该有这个:

printf("P1d1 = %d\nP1d2 = %d\nP2d1 = %d\n P2d2 = %d\n", P1d1, P1d2, P2d1, P2d2);
于 2013-09-27T06:24:37.230 回答