在 int main() 中一个一个或两个地使用函数

Using Functions One by One or both in int main()

所以,我得到了 2 个函数,它们基本上都在玩一个名为 "Pig Game" 的游戏,下面的代码是 int main() 在我的代码中,它只打印主要内容,然后一个一个地启动函数。此外,还有一个名为 roll_a_dice() 的函数,您可以忽略它,它基本上是掷骰子。

例如,在第一个 while 循环中,play_computer 首先开始,play_user 第二。循环重复 6 次。 而我需要的是每一轮之后,我需要得到函数的结果(或输出,或return)和把它放在另一个名为 scoresheets() 的函数中。我不知道该怎么做。请帮助我。

int main(void){
int roll, comp, me, round=1;
srand(time(NULL));
printf("Welcome to Big Pig game.");
printf("\nLets get started!");
comp = roll_a_dice();
printf("\nI have rolled the dice and got %d!",comp);
printf("\nShall i roll the dice for you (Y/N)? ");
scanf("%c",&roll);
if (roll=='Y'){
    me=roll_a_dice();
    printf("I have rolled the dice for you and you got %d!",me);
    if (comp>me){
        while (round<=6){
        printf("\nRound %d--My Turn: ",round);
        printf("\n===================================================================================");
        printf("%d",play_computer());
        printf("\nRound %d--Your Turn: ",round);
        printf("\n===================================================================================");
        printf("%d",play_user());
        round++;    
        }
        }
    else{
        while (round<=6){
        printf("\nRound %d--Your Turn: ",round);
        printf("\n===================================================================================");
        printf("%d",play_user());
        printf("\nRound %d--My Turn: ",round);
        printf("\n===================================================================================");
        printf("%d",play_computer());
        round++;    
        }
        }
    }
printf("%d",scoresheet());
return 0;

}

另外,我稍微修改了你的代码

//Included the header files which I assume you included in your program so that it compiles

#include <stdio.h>
#include <stddef.h>
#include <stdlib.h> 
#include <time.h>
#include <stdlib.h>



//Declaring the functions



int scoresheet(int getOrPut,int score, int player) 
{
    //0 is put and 1 is get
    static int playerScore=0;
    static int computerScore=0;
    if(player==0 && getOrPut==0)
    {
        playerScore+=score;
        return 0;
    }
    if(player==1 && getOrPut==0)
    {
        computerScore+=score;
        return 0;
    }
    if(player==0 && getOrPut==1)
        return playerScore;
    if(player==1 && getOrPut==1)
        return computerScore;
    return -1;
}


int roll_a_dice()
{
    return (rand()%6) +1; //I assume the code looks something like this
}


int play_user(int round) //Using parameters
{

    int totalScoreOfUser=(rand()%36) + 1; //Enter code to calculate actual score here 
    scoresheet(0,totalScoreOfUser,0);//Add score to scoresheet
    return totalScoreOfUser;
}


int play_computer() //Without passing parameters
{
    static int round=0; //Declares a variable static to the function
    round++;//Increases value of variable each time function is called
    int totalScoreOfComputer=(rand()%36) + 1; 
    scoresheet(0,totalScoreOfComputer,1);//Add score to scoresheet
    return totalScoreOfComputer; 
}




int main(void)
{
    int comp, me, round=1;
    char roll; //changed type of roll from int to char as you were scanning input as %c
    srand(time(NULL));
    printf("Welcome to Big Pig game.");
    printf("\nLets get started!");
    comp = roll_a_dice();
    printf("\nI have rolled the dice and got %d!",comp);
    printf("\nShall I roll the dice for you (Y/N)? "); //Fixed grammar changing i to I in string
    scanf(" %c",&roll); //Added a blank space before %c to remove whitespace errors during run time

    while(roll!='Y' && roll!='y' && roll!='N' && roll!='n') //Added some extra code so that user will always input either y or n
    {
        printf("\nInvalid Input. Please enter either (Y/N)\n");
        scanf(" %c",&roll);
    }

    if (roll=='Y' || roll=='y'){ //Most people don't usually press shift while typing so included the lower case y in your code
            me=roll_a_dice();
            printf("I have rolled the dice for you and you got %d!",me);
            if (comp>me){ //When computer has rolled a higher number, it goes first
                while (round<=6){
                //I reordered and combined a few print statements so the code looks shorter
                    printf("\nRound %d--My Turn: ",round);
                    printf("%d",play_computer());
                    printf("\n===================================================================================");
                    printf("\nRound %d--Your Turn: ",round);

                    printf("%d",play_user(round));
                printf("\n===================================================================================");        
                round++;    
                }
            }
        else{ //When user has rolled higher or both rolled equal, user goes first
                while (round<=6){
                //I reordered and combined a few print statements so the code looks shorter
                printf("\nRound %d--Your Turn: %d",round,play_user(round));
                printf("\n===================================================================================");        

                printf("\nRound %d--My Turn: %d",round,play_computer());        
                printf("\n===================================================================================");   
                    round++;    
                }
            }
        printf("\nTotal Scores are Computer: %d and User: %d\n",scoresheet(1,0,1),scoresheet(1,0,0));

        }

    else
    {
        printf("You chose not to roll the die and hence the game did not begin\n");
    }

    return 0;
}