初学者:C程序不添加



在"for"语句下,我试图做的一件事是添加两个骰子,如果它们加起来都是15个,只需计算它们。程序运行后打印两个骰子的次数加起来为 15。

错误:总和始终为 0。

#include <stdio.h>

int main()
{

int SEED, TIMES_ROLL,COUNT,DICE1,DICE2,PAIRS,SUM, SUM_COUNT;

//Ask user for seed value
printf("Type in a number for the seed value?n");
scanf("%d", &SEED);
srand(SEED);
SUM = 0;
SUM_COUNT = 0;
//Ask user how many times to roll the 2 dice
printf("How many times would you like to roll the dice?n");
scanf("%d", &TIMES_ROLL);
printf("ROLLING THE DICE..............n");
**for (COUNT = 1; COUNT < TIMES_ROLL +1;++COUNT)
{
DICE1 = rand() % 6 + 1;
DICE2 = rand() % 6 + 1;
if (COUNT <=10)
printf("%d and %d rolledn", DICE1,DICE2);
if (DICE1 == DICE2)
PAIRS = PAIRS + 1;
SUM =  DICE1 + DICE2;
if (SUM == 15)
SUM_COUNT = SUM_COUNT + 1;**

}
printf("(%d WERE PAIRS)n",PAIRS);
printf("(15 WAS THE SUM OF BOTH PAIRS %d TIMES)n", SUM_COUNT);
return 0;
}

变量SUM永远不会15为变量DICE1的范围,并且两者DICE21...6,并且它们的总和范围为2...12

这就是为什么条件if (SUM == 15)总是假的,变量SUM不会改变。

最新更新