char userChoice;
printf("Choose how you would like to search.\nEnter A to display all players information.\
\nEnter N to search a player based on name.\nEnter J to search a player based on jersey number.\
\nEnter P to search a player based on position.\nEnter your choice: ");
scanf("%c", &userChoice);
do
{
if (userChoice == 'A' || userChoice == 'a')
{
for (index = 0; index < count; index = index + 1)
{
displayPlayers(&players[index]);
}
}
if (userChoice == 'J' || userChoice == 'j')
{
int jerseyNumber;
printf("\nEnter jersey number for the player: ");
scanf("%i", &jerseyNumber);
for (index = 0; index <= MAX_PLAYERS; index++)
{
if (jerseyNumber == players[index].jerseyNumber)
{
// If the condition is met the singleDisplay function is called.
// Containing the array of struct
singleDisplay(&players[index]);
}
}
}
if (userChoice == 'N' || userChoice == 'n')
{
char playerName[LEN_NAME + 1];
printf("\nEnter name for the player: ");
scanf("%s", playerName);
for (index = 0; index <= MAX_PLAYERS; index++)
{
if (strcmp(playerName, players[index].firstName) == 0)
{
singleDisplay(&players[index]);
}
}
}
大部分代碼僅適用於上下文,我遇到的問題是無法生成一個else語句,輸出一條消息給用戶,他們輸入的澤西島沒有找到。問題在於else語句在循環內部,並且無論多次打印它的消息,它都會比較數組中的所有數字。For循環IF語句C編程
您應該查看C語言中的'switch'語句,它可以在這裏很好地工作。 – lurker
'for(index = 0; index
BLUEPIXY
那裏沒有「else語句」。再次閱讀你的問題。 –