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

py49o6xq  于 2023-04-19  发布在  其他
关注(0)|答案(2)|浏览(104)

我必须做一个石头剪刀布游戏,我在尝试如何从player()获取用户输入和从computer()game()的随机生成值时遇到了麻烦,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()?也许使用指针是答案,但我还不太了解它们。

fwzugrvs

fwzugrvs1#

首先,下面的代码有一个错误computer_choice is not declared,应该转到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);
}

第三,你的很多函数不需要有参数,比如:电脑..
最后我把你的代码改成:

#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");
    }
}

希望能帮上忙

kuuvgm7e

kuuvgm7e2#

我认为你混淆了函数和参数的概念。例如,你有一个函数“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';
}

相关问题