为什么它不输入一些 else-if 语句是有原因的?
Is there a reason for why it doesn't enter some of the else-if statements?
一些 else-if 语句不像 'else if(strcmp(player1,player2) == 0)
' 或 'else if(strcmp(player1, "scissors") == 0)'。会不会是他们后来的'if'声明?它们是另一个有效的 else if 语句的副本(经过一些修改)。它们几乎相同,所以我不明白为什么它们不起作用。
#include <stdio.h>
#include <string.h>
int main(void) {
char player1[25];
char player2[25];
while(1){
printf("please type one of the following: rock, paper or scissors. Player1 goes first\n");
scanf("%24s%24s",player1,player2);
if (strcmp(player1, "rock") == 0)
{
if (strcmp(player2, "scissors") == 0)
printf("player 1 wins, rock crushes scissors\n");
}
else if(strcmp(player1, "rock") == 0)
{
if (strcmp(player2, "paper") == 0)
printf("player 2 wins, paper covers rock\n");
}
else if(strcmp(player1, "scissors") == 0)
{
if(strcmp(player2, "paper") == 0)
printf("player 1 wins, scissor cuts paper\n");
}
else if(strcmp(player1, "scissors") == 0)
{
if(strcmp(player2, "rock") == 0)
printf("player 2 wins, rock crushes scissors\n");
}
else if(strcmp(player1, "paper") == 0)
{
if(strcmp(player2, "rock") == 0)
printf("player 1 wins, paper covers rock\n");
}
else if(strcmp(player1, "paper") == 0)
{
if(strcmp(player2, "scissors") == 0)
printf("player 2 wins, scissor cuts paper\n");
}
else if(strcmp(player2, "rock") == 0)
{
if (strcmp(player1, "scissors") == 0)
printf("player 2 wins, rock crushes scissors\n");
}
else if(strcmp(player2, "rock") == 0)
{
if (strcmp(player1, "paper") == 0)
printf("player 1 wins, paper covers rock\n");
}
else if(strcmp(player2, "scissors") == 0)
{
if(strcmp(player1, "paper") == 0)
printf("player 2 wins, scissor cuts paper\n");
}
else if(strcmp(player2, "scissors") == 0)
{
if(strcmp(player1, "rock") == 0)
printf("player 1 wins, rock crushes scissors\n");
}
else if(strcmp(player2, "paper") == 0)
{
if(strcmp(player1, "rock") == 0)
printf("player 2 wins, paper covers rock\n");
}
else if(strcmp(player2, "paper") == 0)
{
if(strcmp(player1, "scissors") == 0)
printf("player 1 wins, scissor cuts paper\n");
}
else if(strcmp(player1,player2) == 0)
{
printf("nobody wins, let’s sing it again!\n");
}
else
printf("incorrect input, try again\n");
}
return 0;
}
您的 if-else 语句没有意义。例如考虑这些 if-else 语句
else if(strcmp(player1, "scissors") == 0)
{
if(strcmp(player2, "paper") == 0)
printf("player 1 wins, scissor cuts paper\n");
}
else if(strcmp(player1, "scissors") == 0)
{
if(strcmp(player2, "rock") == 0)
printf("player 2 wins, rock crushes scissors\n");
}
两个 else if 语句具有相同的条件
else if(strcmp(player1, "scissors") == 0)
//...
else if(strcmp(player1, "scissors") == 0)
所以第二个 else if 语句要么不会被执行,要么它的条件永远不会等于 true。
例如,您可以按以下方式编写单独的 if 语句
if (strcmp(player1, "rock") == 0)
{
if (strcmp(player2, "scissors") == 0)
printf("player 1 wins, rock crushes scissors\n");
else if (strcmp(player2, "paper") == 0)
printf("player 2 wins, paper covers rock\n");
}
else if(strcmp(player1, "scissors") == 0)
{
if(strcmp(player2, "paper") == 0)
printf("player 1 wins, scissor cuts paper\n");
else if(strcmp(player2, "rock") == 0)
printf("player 2 wins, rock crushes scissors\n");
}
//...
一些 else-if 语句不像 'else if(strcmp(player1,player2) == 0) ' 或 'else if(strcmp(player1, "scissors") == 0)'。会不会是他们后来的'if'声明?它们是另一个有效的 else if 语句的副本(经过一些修改)。它们几乎相同,所以我不明白为什么它们不起作用。
#include <stdio.h>
#include <string.h>
int main(void) {
char player1[25];
char player2[25];
while(1){
printf("please type one of the following: rock, paper or scissors. Player1 goes first\n");
scanf("%24s%24s",player1,player2);
if (strcmp(player1, "rock") == 0)
{
if (strcmp(player2, "scissors") == 0)
printf("player 1 wins, rock crushes scissors\n");
}
else if(strcmp(player1, "rock") == 0)
{
if (strcmp(player2, "paper") == 0)
printf("player 2 wins, paper covers rock\n");
}
else if(strcmp(player1, "scissors") == 0)
{
if(strcmp(player2, "paper") == 0)
printf("player 1 wins, scissor cuts paper\n");
}
else if(strcmp(player1, "scissors") == 0)
{
if(strcmp(player2, "rock") == 0)
printf("player 2 wins, rock crushes scissors\n");
}
else if(strcmp(player1, "paper") == 0)
{
if(strcmp(player2, "rock") == 0)
printf("player 1 wins, paper covers rock\n");
}
else if(strcmp(player1, "paper") == 0)
{
if(strcmp(player2, "scissors") == 0)
printf("player 2 wins, scissor cuts paper\n");
}
else if(strcmp(player2, "rock") == 0)
{
if (strcmp(player1, "scissors") == 0)
printf("player 2 wins, rock crushes scissors\n");
}
else if(strcmp(player2, "rock") == 0)
{
if (strcmp(player1, "paper") == 0)
printf("player 1 wins, paper covers rock\n");
}
else if(strcmp(player2, "scissors") == 0)
{
if(strcmp(player1, "paper") == 0)
printf("player 2 wins, scissor cuts paper\n");
}
else if(strcmp(player2, "scissors") == 0)
{
if(strcmp(player1, "rock") == 0)
printf("player 1 wins, rock crushes scissors\n");
}
else if(strcmp(player2, "paper") == 0)
{
if(strcmp(player1, "rock") == 0)
printf("player 2 wins, paper covers rock\n");
}
else if(strcmp(player2, "paper") == 0)
{
if(strcmp(player1, "scissors") == 0)
printf("player 1 wins, scissor cuts paper\n");
}
else if(strcmp(player1,player2) == 0)
{
printf("nobody wins, let’s sing it again!\n");
}
else
printf("incorrect input, try again\n");
}
return 0;
}
您的 if-else 语句没有意义。例如考虑这些 if-else 语句
else if(strcmp(player1, "scissors") == 0)
{
if(strcmp(player2, "paper") == 0)
printf("player 1 wins, scissor cuts paper\n");
}
else if(strcmp(player1, "scissors") == 0)
{
if(strcmp(player2, "rock") == 0)
printf("player 2 wins, rock crushes scissors\n");
}
两个 else if 语句具有相同的条件
else if(strcmp(player1, "scissors") == 0)
//...
else if(strcmp(player1, "scissors") == 0)
所以第二个 else if 语句要么不会被执行,要么它的条件永远不会等于 true。
例如,您可以按以下方式编写单独的 if 语句
if (strcmp(player1, "rock") == 0)
{
if (strcmp(player2, "scissors") == 0)
printf("player 1 wins, rock crushes scissors\n");
else if (strcmp(player2, "paper") == 0)
printf("player 2 wins, paper covers rock\n");
}
else if(strcmp(player1, "scissors") == 0)
{
if(strcmp(player2, "paper") == 0)
printf("player 1 wins, scissor cuts paper\n");
else if(strcmp(player2, "rock") == 0)
printf("player 2 wins, rock crushes scissors\n");
}
//...