2016-06-15 10 views
0

Unter der "for" -Anweisung ist eines der Dinge im Versuch zu tun, zwei Würfel zu addieren und wenn sie beide bis zu 15 addieren, zählen Sie sie einfach. Nachdem das Programm Druck die Anzahl der Male läuft beide Würfel addieren sich zu 15Anfänger: C-Programm nicht hinzufügen

Fehler: Die Summe ist immer 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 rolled\n", 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; 

} 

Antwort

0

Variable SUM wird für Variablen nie 15 als Bereich DICE1 und DICE2 ist 1...6 für beide, und ihr Summenbereich ist 2...12.

Deshalb ist die Bedingung if (SUM == 15) immer falsch und SUM Variable wird nicht geändert.

+1

Und deshalb ist Schlaf sehr wichtig, danke! – user001