每当我编译我的代码时,无论传递什么整数,它都会打印所有可能的场景。有关如何解决这个问题以及为什么会发生这种情况的任何帮助?
#include <stdio.h>
void *get_plays(char *player1, char *player2)
{
printf ("Player 1 chooses to play: ");
scanf("%s", player1);
printf ("Player 2 chooses to play: ");
scanf("%s", player2);
return player1;
return player2;
}
int *check_for_winner(char *player1, char *player2, int condition)
{
if (player1==player2){
condition = 0;
}
else if(player1=="rock" && player2 == "scissors"){
condition = 1;
}
else if(player1=="paper" && player2 == "scissors"){
condition = 2;
}
else if(player1=="rock" && player2 == "paper"){
condition = 2;
}
else if(player1=="paper" && player2 == "rock"){
condition = 1;
}
else if(player1=="scissors" && player2 == "papper"){
condition = 1;
}
else (player1=="scissors" && player2 == "rock");{
condition = 1;
}
}
void *print_plays(char *player1, char *player2)
{
printf("Player 1 has played %s \n",player1);
printf("Player 2 has played %s \n",player2);
}
void *print_winner(int condition)
{
if (condition = 0){
printf("It's a tie\n");
}
else if (condition = 1){
printf("Player 1 wins\n");
}
else (condition = 2);{
printf("Player 2 wins\n");
}
}
int main(void)
{
char player1[10];
char player2[10];
int win;
get_plays(player1, player2);
check_for_winner(player1, player2, win);
print_plays(player1, player2);
print_winner(win);
return;
}
当我编译它时,打印“播放器1获胜”和“播放器2获胜”没有线索为什么或如何解决它甚至为什么它发生。我对C仍然相当新,所以任何帮助都会受到赞赏。谢谢。
答案 0 :(得分:0)
#include <stdio.h>
#include <string.h>
void
get_plays (char *choice1, char *choice2)
{
printf ("Choices: 'paper' - 'rock' - 'scissors'\n");
printf ("Player 1 chooses to play: ");
scanf ("%s", choice1);
printf ("Player 2 chooses to play: ");
scanf ("%s", choice2);
}
int
check_for_winner (const char *choice1, const char *choice2)
{
// paper wins rock but scissors does not and equals itself
if (strcmp (choice1, "paper") == 0)
{
if (strcmp (choice2, "rock") == 0)
return 1;
if (strcmp (choice2, "scissors") == 0)
return 2;
return 0;
}
// rock wins scissors but paper does not and equals itself
if (strcmp (choice1, "rock") == 0)
{
if (strcmp (choice2, "scissors") == 0)
return 1;
if (strcmp (choice2, "paper") == 0)
return 2;
return 0;
}
// scissors wins paper but rock does not and equals itself
if (strcmp (choice1, "scissors") == 0)
{
if (strcmp (choice2, "paper") == 0)
return 1;
if (strcmp (choice2, "rock") == 0)
return 2;
return 0;
}
return 0;
}
int main ()
{
char choice1 [10];
char choice2 [10];
int winner;
char q;
char *result[3] = {
"No one is the winner",
"Player 1 is the winner!!!",
"Player 2 is the winner!!!"
};
printf ("Choose 'q' to exit\n");
printf ("------------------------------------------------------------\n");
while (q != 'q')
{
get_plays (&choice1[0], &choice2[0]);
winner = check_for_winner (choice1, choice2);
printf ("------------------------------------------------------------\n");
printf ("The result: %s\n", result[winner]);
printf ("------------------------------------------------------------\n");
getc (stdin);
scanf ("%c", &q);
}
return 0;
}
我希望代码能为您提供帮助!