gpt4 book ai didi

C - 剪刀石头布计分板问题

转载 作者:太空宇宙 更新时间:2023-11-04 07:57:29 25 4
gpt4 key购买 nike

语言:C

情况:我们的教授让我们制作一个剪刀石头布代码,其中包含与主函数不同的函数,还包括一个“switch”语句、“for”循环和一个“if.. .else”语句。

问题:该程序大部分工作,但在退出时无法为记分牌显示正确的金额。它似乎忽略了手头之前的所有内容并默认返回原始定义值“0”

我的 C 代码如下:

#include "stdio.h"
#include "stdlib.h"
#include "time.h"

// Rock
int rock(int comp, int Tie, int Cwon, int Pwon) {
if(comp==1) {
printf("\nTie!\n");
Tie++;
} else if(comp==2) {
printf("\nYou picked Rock, the computer picked Paper.\nPaper covers Rock, the computer wins!\n");
Cwon++;
} else if(comp==3) {
printf("\nYou picked Rock, the computer picked Scissors.\nRock breaks Scissors, you win!\n");
Pwon++;
} else {
printf("Error");
}
return Tie, Cwon, Pwon;
}

// Paper
int paper(int comp, int Tie, int Cwon, int Pwon) {
if(comp==1) {
printf("\nYou picked Paper, the computer picked Rock.\nPaper covers Rock, you win!\n");
Pwon++;
} else if(comp==2) {
printf("\nTie!\n");
Tie++;
} else if(comp==3) {
printf("\nYou picked Paper, the computer picked Scissors.\nScissors cut Paper, the computer wins!\n");
Cwon++;
} else {
printf("Error");
}
return Tie, Cwon, Pwon;
}

// Scissors
int scissors(int comp, int Tie, int Cwon, int Pwon) {
if(comp==1) {
printf("\nYou picked Scissors, the computer picked Rock.\nRock breaks Scissors, the computer wins!\n");
Cwon++;
} else if(comp==2) {
printf("\nYou picked Scissors, the computer picked Paper.\nScissors cut Paper, you win!\n");
Pwon++;
} else if(comp==3) {
printf("\nTie!\n");
Tie++;
} else {
printf("Error");
}
return Tie, Cwon, Pwon;
}

// Score Display
int score(int Pwon, int Cwon, int Tie) {
printf("\nYou won %d times, the computer won %d times and it was a tie %d times.\nThank you for playing!\n", Pwon, Cwon, Tie);
}

// playCases w/ Switch
int playCases(char input, int comp, int Tie, int Cwon, int Pwon, int i) {
switch(input) {
case 'R' :
case 'r' :
rock(comp, Tie, Cwon, Pwon);
break;
case 'P' :
case 'p' :
paper(comp, Tie, Cwon, Pwon);
break;
case 'S' :
case 's' :
scissors(comp, Tie, Cwon, Pwon);
break;
case 'Q' :
case 'q' :
score(Pwon, Cwon, Tie);
i=999;
break;
default :
printf("Error: Invalid choice, try again.");
break;
}
return Tie, Cwon, Pwon;
}

// Flush
void flushScanf(int input) {
char r;
while((r = getchar()) != '\n' && r != EOF);
}

// Main Function
int main(void) {
char input;
int Cwon=0, Pwon=0, Tie=0;
printf("Let's play a game of Rock/Paper/Scissors\n");

//For Loop
for(int i=1; i<999;++i) {
printf("\nEnter the r, p, s, or q (for quit): ");
scanf("%c", &input);
flushScanf(input);
int comp=rand()%3+1;
playCases(input, comp, Tie, Cwon, Pwon, i);
}
return 0;
}

我对编码还是比较陌生,所以如果我的代码看起来一团糟而且没有按应有的方式压缩,我深表歉意。

最佳答案

有几个问题。了解如何通过指针传递变量。如果这样做,您可以修改函数内的值。

 int paper(int comp, int Tie, int Cwon, int Pwon) {

替换为:

int paper(int comp, int *Tie, int *Cwon, int *Pwon) {
//...
(*Cwon)++;
//...
}

你不能像这样返回相乘的值:

 return Tie, Cwon, Pwon;

正如我提到的,使用指针并在指针被 * 运算符取消引用后修改值。

这是供您考虑的小示例。请注意,num 在函数 scissors 之外不会发生变化。

#include<stdio.h>

void scissors(int num, int *Tie, int *Cwon, int *Pwon) {

num++;

(*Tie)++;
(*Cwon)++;
(*Pwon)++;
}

int main() {
int num = 0;
int Tie = 1;
int Cwon= 2;
int Pwon= 3;

scissors(num, &Tie, &Cwon, &Pwon);
printf("num= %d Tie= %d Cwon= %d Pwon= %d \n", num, Tie, Cwon, Pwon );

scissors(num, &Tie, &Cwon, &Pwon);
printf("num= %d Tie= %d Cwon= %d Pwon= %d \n", num, Tie, Cwon, Pwon );

return 0;
}

输出:

num= 0 Tie= 2 Cwon= 3  Pwon= 4 
num= 0 Tie= 3 Cwon= 4 Pwon= 5

关于C - 剪刀石头布计分板问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48996440/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com