You may have written scissors games when you were a beginner of C language. However, when writing scissors functions, you cannot avoid using loops.
When a function is caught in a loop, your scoring variable may be reset to the initial value in the function body. So how to solve this problem?
In fact, it is very simple, you need to add a cumulative variable in the loop body, so that the function body of the single-loop scoring through the main function of the loop to become the final total integral, and thus to determine the end of your points and win.
Here is some of the code I wrote, please correct me!
1. Function Body part
intCompareintXinty) { Int j = Each loop outside the 0;//function initializes the value of Jif(x<3&&x>0){ if(y-x==0) {printf ("This scissors two people to play a tie! "); } if(y-x==1|| x-y==1){ if(x>y) {printf ("you won the scissors this time! Add a point! "); J++; } Else{printf ("this scissors computer wins! Minus one point! "); J--; } } if(y-x==2|| x-y==2){ if(x>y) {printf ("this scissors computer wins! Minus one point! "); J--; } Else{printf ("you won the scissors this time! Add a point! "); J++; } } } Else{printf ("you entered an error! \ n"); } return j;//return value is initialized in each main function loop }
2.main body
for(i=0;i<5; i++) {printf ("\ n"); printf ("Please enter your punch! "); scanf ("%d",&x); Y= rand ()%3; printf ("computer punch:%d\n", y); Ary[i]=Compare (x, y); sum +=Ary[i]; //With the sum variable it is easy to solve the problem of initialization of scoring variables }
C Language Writing Scissors games encountered in the function of the cycle of small problems