C 在int main()中逐个或同时使用函数
因此,我得到了两个函数,它们基本上都在玩一个名为“Pig game”的游戏,下面的代码在我的代码中是int main(),它只是打印主要内容,然后逐个启动函数。另外,还有另一个函数名为掷骰子()您可以忽略它,它基本上是掷骰子 例如,在第一个while循环中,play_计算机首先启动,play_用户第二次启动。循环重复6次我需要的是在每一轮之后,我需要得到函数的结果(或输出,或返回),并将其放入另一个名为scoresheets()的函数中。我不知道怎么做。请帮帮我C 在int main()中逐个或同时使用函数,c,function,if-statement,while-loop,main,C,Function,If Statement,While Loop,Main,因此,我得到了两个函数,它们基本上都在玩一个名为“Pig game”的游戏,下面的代码在我的代码中是int main(),它只是打印主要内容,然后逐个启动函数。另外,还有另一个函数名为掷骰子()您可以忽略它,它基本上是掷骰子 例如,在第一个while循环中,play_计算机首先启动,play_用户第二次启动。循环重复6次我需要的是在每一轮之后,我需要得到函数的结果(或输出,或返回),并将其放入另一个名为scoresheets()的函数中。我不知道怎么做。请帮帮我 int main(void){
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;
int main(无效){
整卷,复合,me,整轮=1;
srand(时间(空));
printf(“欢迎来到大猪游戏”);
printf(“\nLets get started!”);
comp=掷骰子();
printf(“\n我掷骰子得到%d!”,comp);
printf(“\N我为您掷骰子(Y/N)?”;
scanf(“%c”、&roll);
如果(滚动=='Y'){
我=掷骰子();
printf(“我已经为你掷了骰子,你得到了%d!”,我);
如果(comp>me){
同时(round我也稍微修改了你的代码
//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;
}
//包含头文件,我假设您将其包含在程序中,以便它进行编译
#包括
#包括
#包括
#包括
#包括
//声明函数
智力得分表(智力得分,智力得分,智力球员)
{
//0是put,1是get
静态int playerScore=0;
静态积分=0;
如果(player==0&&getOrPut==0)
{
playerScore+=得分;
返回0;
}
如果(player==1&&getOrPut==0)
{
计算机分数+=分数;
返回0;
}
如果(player==0&&getOrPut==1)
返回playerScore;
如果(player==1&&getOrPut==1)
返回计算机分数;
返回-1;
}
int roll_a_dice()
{
return(rand()%6)+1;//我假设代码如下所示
}
int play_user(int round)//使用参数
{
int totalScoreOfUser=(rand()%36)+1;//在此处输入代码以计算实际分数
记分表(0,totalScoreOfUser,0);//将分数添加到记分表
返回用户总数;
}
int play_computer()//不传递参数
{
static int round=0;//向函数声明一个静态变量
round++;//每次调用函数时都增加变量的值
int totalScoreOfComputer=(rand()%36)+1;
记分表(0,totalScoreOfComputer,1);//将分数添加到记分表
返回计算机的总计数;
}
内部主(空)
{
整数比较,me,四舍五入=1;
char roll;//在扫描输入%c时,将roll的类型从int更改为char
srand(时间(空));
printf(“欢迎来到大猪游戏”);
printf(“\nLets get started!”);
comp=掷骰子();
printf(“\n我掷骰子得到%d!”,comp);
printf(“\N我为你掷骰子(Y/N)?”;//修复了将字符串中的I更改为I的语法
scanf(“%c”,&roll);//在%c之前添加了一个空格,以在运行时删除空格错误
while(roll!='Y'&&roll!='Y'&&roll!='N'&&roll!='N')//添加了一些额外的代码,以便用户始终输入Y或N
{
printf(“\N无效输入。请输入(Y/N)\N”);
scanf(“%c”、&roll);
}
if(roll=='Y'| | roll=='Y'){//大多数人在键入时通常不按shift键,因此在代码中包含小写的Y
我=掷骰子();
printf(“我已经为你掷了骰子,你得到了%d!”,我);
如果(comp>me){//当计算机滚动到一个更高的数字时,它会先滚动
虽然请澄清大猪游戏的规则。规则是:开始时,它为玩家和计算机随机掷骰子,获得较大数字的玩家先开始。先开始的玩家掷2个骰子,结果就是这样。最后,如果玩家或计算机掷1或按N,则停止并返回游戏的总分这一轮。第1轮:玩家然后是电脑,第2轮:玩家然后是电脑……进入第6轮。我需要得到记分表()的总分。