如何从函数中获取输入以在另一个函数中使用它

  • 本文关键字:函数 另一个 获取 c
  • 更新时间 :
  • 英文 :


我必须制作一款石头剪子布游戏,我在尝试如何从player()获取用户输入和从computer()game()随机生成的值时遇到了麻烦,这应该决定返回值。(game()->outcome())此外,我不允许在main()中接受用户输入。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
char computer(char);
char player(char);
int game(char, char);
void outcome(char, char);
int main()
{
char p, c;
computer(c);
player(p);
game(p, c);
outcome(p, c);
}
char computer(char comp_choice) {

srand(time(0));
int n = (rand() % 3) + 1;

if(n == 1) {
comp_choice = 'R';
}
else if(n == 2) {
comp_choice = 'P';
}
else {
comp_choice = 'S';
}

return comp_choice;
}
char player(char player_choice) {

printf("Enter(R)ock/(P)aper/(S)cissors:");
scanf("%c", &player_choice);

return player_choice;
}
int game(char player_choice, char comp_choice)
{

if(player_choice == 'R' && computer_choice == 'S') {
return -1;
}
else if(player_choice == 'S' && comp_choice == 'P') {
return -1;
}
else if(player_choice == 'P' && comp_choice == 'R') {
return -1;
}
else if(player_choice == 'S' && comp_choice == 'R') {
return 0;
}
else if(player_choice == 'P' && comp_choice == 'S') {
return 0;
}
else if(player_choice == 'R' && comp_choice == 'P') {
return 0;
}
else if(player_choice == comp_choice) {
return 1;
}
else {
return 2;
}
}  
void outcome(char p, char c) {

int result = game(p, c);

if(result == -1) {
printf("You Win");
}
else if(result == 0) {
printf("You lose");
}
else if(result == 1) {
printf("It's a tie");
}
}

我试图做if (player(player_choice) == 'R' && computer(comp_choice) == 'S',但我认为运行player()不止一次。我能做些什么来正确地将值传递给game()?也许使用指针是答案,但我还不太了解它们。

首先下面的代码有一个错误computer_choice没有声明,应该转向comp_choice

if(player_choice == 'R' && computer_choice == 'S') {
return -1;
}
其次,下面代码中使用的每个变量都没有初始化:
int main()
{
char p, c;
computer(c);
player(p);
game(p, c);
outcome(p, c);
}

第三,很多函数不需要参数,比如:computer ..

最后我把你的代码改成:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
char computer();
char player();
int game(char, char);
void outcome(int);
int main()
{
outcome(game(computer(), player()));
}
char computer(void) {
char comp_choice;
srand(time(0));
int n = (rand() % 3) + 1;
if(n == 1) {
comp_choice = 'R';
}
else if(n == 2) {
comp_choice = 'P';
}
else {
comp_choice = 'S';
}
return comp_choice;
}
char player(void) {
char player_choice;
printf("Enter(R)ock/(P)aper/(S)cissors:");
scanf("%c", &player_choice);
return player_choice;
}
int game(char player_choice, char comp_choice)
{
if(player_choice == 'R' && comp_choice == 'S') {
return -1;
}
else if(player_choice == 'S' && comp_choice == 'P') {
return -1;
}
else if(player_choice == 'P' && comp_choice == 'R') {
return -1;
}
else if(player_choice == 'S' && comp_choice == 'R') {
return 0;
}
else if(player_choice == 'P' && comp_choice == 'S') {
return 0;
}
else if(player_choice == 'R' && comp_choice == 'P') {
return 0;
}
else if(player_choice == comp_choice) {
return 1;
}
else {
return 2;
}
}
void outcome(int result) {
if(result == -1) {
printf("You Win");
}
else if(result == 0) {
printf("You lose");
}
else if(result == 1) {
printf("It's a tie");
}
}

希望有帮助

我想你混淆了函数和参数的概念。例如,你有一个函数"char Player()"它必须返回一个char值并且没有参数。在该函数中,您将请求用户输入,然后返回它。那么在另一个函数中你会有一个等于这个函数的变量,像这样

int main() {
char choice = player();
}
char player() {
char choice;
printf("Enter(R)ock/(P)aper/(S)cissors:");
scanf("%c", &choice);

return choice;
}

此外,如果你想让一个函数计算结果,你可以使用参数

int main() {
char result = result('P', 'S'); //Result will be equal to computer
}
char result(char player, char comp) {
if(player == 'S' && comp == 'P'){
return 'W'
}else if(player == 'P' && comp == 'S'){
return 'L'
}//...More conditions to be added
return 'E';
}

相关内容

  • 没有找到相关文章

最新更新