C Language Writing Scissors games encountered in the function of the cycle of small problems

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.