英文:
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 <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");
}
}
I tried to do if (player(player_choice) == 'R' && computer(comp_choice) == 'S'
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("Enter(R)ock/(P)aper/(S)cissors:");
scanf("%c", &choice);
return choice;
}
Moreover if you want to have a function calculating the result you can use parameters
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';
}
答案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 == 'R' && computer_choice == 'S') {
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 <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");
}
}
Hope that helps
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论