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

huangapple go评论61阅读模式
英文:

How to take input from a function to use it in another function

问题

Sure, here's the translated code part:

我需要制作一个剪刀石头布游戏,但我遇到了如何从 `player()` 获取用户输入和从 `computer()` 生成的随机值传递到 `game()` 的问题,`game()` 应该确定返回值。 (可能在 `game()`  `outcome()` 也有相同的问题)同时,我不允许在 `main()` 中获取用户输入。

```c
#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;
   c = computer(c);
   p = 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("输入(R)剪刀 / (P)石头 / (S)布:");
    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(char p, char c) {
    
    int result = game(p, c);
    
    if(result == -1) {
        printf("你赢了");
    }
    else if(result == 0) {
        printf("你输了");
    }
    else if(result == 1) {
        printf("平局");
    }
}

我尝试使用 if (player(player_choice) == 'R' && computer(comp_choice) == 'S',但我认为这会多次运行 player()。要正确传递值给 game(),你可以像这样更改 main()

int main()
{
   char p, c;
   c = computer(c);
   p = player(p);
   int result = game(p, c);
   outcome(p, c, result);
}

还需要修改 outcome() 函数以接受结果值,并相应地输出结果。

英文:

I have to make a Rock Paper Scissors game, and I'm having trouble trying to how to take user input from player()and randomly generated value from computer()to game(), which is supposed to determine the return value. (Might have a same problem with game() --> outcome()) Also, I'm not allowed to take user inputs in main().

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;time.h&gt;
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 = &#39;R&#39;;
}
else if(n == 2) {
comp_choice = &#39;P&#39;;
}
else {
comp_choice = &#39;S&#39;;
}
return comp_choice;
}
char player(char player_choice) {
printf(&quot;Enter(R)ock/(P)aper/(S)cissors:&quot;);
scanf(&quot;%c&quot;, &amp;player_choice);
return player_choice;
}
int game(char player_choice, char comp_choice)
{
if(player_choice == &#39;R&#39; &amp;&amp; computer_choice == &#39;S&#39;) {
return -1;
}
else if(player_choice == &#39;S&#39; &amp;&amp; comp_choice == &#39;P&#39;) {
return -1;
}
else if(player_choice == &#39;P&#39; &amp;&amp; comp_choice == &#39;R&#39;) {
return -1;
}
else if(player_choice == &#39;S&#39; &amp;&amp; comp_choice == &#39;R&#39;) {
return 0;
}
else if(player_choice == &#39;P&#39; &amp;&amp; comp_choice == &#39;S&#39;) {
return 0;
}
else if(player_choice == &#39;R&#39; &amp;&amp; comp_choice == &#39;P&#39;) {
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(&quot;You Win&quot;);
}
else if(result == 0) {
printf(&quot;You lose&quot;);
}
else if(result == 1) {
printf(&quot;It&#39;s a tie&quot;);
}
}

I tried to do if (player(player_choice) == &#39;R&#39; &amp;&amp; computer(comp_choice) == &#39;S&#39; but I think that runs player() more than once. What can I do to properly pass the values to game()? Maybe using pointers is the answer but I do not understand them well enough yet.

答案1

得分: 0

我认为您混淆了函数和参数的概念。例如,您有一个函数 "char Player()",它需要返回一个字符值,并且没有参数。在该函数中,您将要求用户输入,然后返回它。因此,在另一个函数中,您可以将一个变量等于该函数,如下所示:

int main() {
    char choice = player();
}

char player() {
    char choice;
    printf("输入(R)石头/(P)纸/(S)剪刀:");
    scanf("%c", &choice);

    return choice;
}

此外,如果您想要一个计算结果的函数,您可以使用参数:

int main() {
    char result = getResult('P', 'S'); // 结果将等于电脑
}

char getResult(char player, char comp) {
    if (player == 'S' && comp == 'P') {
        return 'W';
    } else if (player == 'P' && comp == 'S') {
        return 'L';
    } // ...要添加更多条件

    return 'E';
}
英文:

I think you have confused the concept of functions and parameters. For example you have a function "char Player()" which has to return a char value and has zero parameters. Into that function you will ask for user input then return it. So in another function you will have a variable equal to that function like this

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

Moreover if you want to have a function calculating the result you can use parameters

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

答案2

得分: 0

以下是你要翻译的代码部分:

首先,下面的代码有一个错误,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(void);
char player(void);
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");
    }
}

希望这有所帮助。

英文:

First the code below has an error computer_choice is not declared and should turn to comp_choice

if(player_choice == &#39;R&#39; &amp;&amp; computer_choice == &#39;S&#39;) {
return -1;
}

Second, every variable used in the code below is not initialized:

int main()
{
char p, c;
computer(c);
player(p);
game(p, c);
outcome(p, c);
}

Third a lot of your functions do not need to have parameters like: computer ..

Finaly i changed your code to:

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;time.h&gt;
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 = &#39;R&#39;;
}
else if(n == 2) {
comp_choice = &#39;P&#39;;
}
else {
comp_choice = &#39;S&#39;;
}
return comp_choice;
}
char player(void) {
char player_choice;
printf(&quot;Enter(R)ock/(P)aper/(S)cissors:&quot;);
scanf(&quot;%c&quot;, &amp;player_choice);
return player_choice;
}
int game(char player_choice, char comp_choice)
{
if(player_choice == &#39;R&#39; &amp;&amp; comp_choice == &#39;S&#39;) {
return -1;
}
else if(player_choice == &#39;S&#39; &amp;&amp; comp_choice == &#39;P&#39;) {
return -1;
}
else if(player_choice == &#39;P&#39; &amp;&amp; comp_choice == &#39;R&#39;) {
return -1;
}
else if(player_choice == &#39;S&#39; &amp;&amp; comp_choice == &#39;R&#39;) {
return 0;
}
else if(player_choice == &#39;P&#39; &amp;&amp; comp_choice == &#39;S&#39;) {
return 0;
}
else if(player_choice == &#39;R&#39; &amp;&amp; comp_choice == &#39;P&#39;) {
return 0;
}
else if(player_choice == comp_choice) {
return 1;
}
else {
return 2;
}
}
void outcome(int result) {
if(result == -1) {
printf(&quot;You Win&quot;);
}
else if(result == 0) {
printf(&quot;You lose&quot;);
}
else if(result == 1) {
printf(&quot;It&#39;s a tie&quot;);
}
}

Hope that helps

huangapple
  • 本文由 发表于 2023年4月11日 12:02:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75982306.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定