【发布时间】:2017-07-22 00:50:10
【问题描述】:
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 语句在循环内部,并且在比较数组中的所有数字时,无论多次打印它的消息。
【问题讨论】:
-
你应该在 C 中查找
switch语句。它在这里会很好用。 -
for(index=0; index<MAX_PLAYERS; index++) { if(jerseyNumber == players[index].jerseyNumber) { singleDisplay(&players[index]); break/* or set Foundflag */; } } if(index == MAX_PLAYERS){ printf("Not found!\n"); -
那里没有“其他陈述”。再次阅读您的问题。
-
else 语句不能正常工作,所以我取出了它。
-
不确定当 switch 语句一次只需要 1 个变量时这将如何工作。Blue idk 将如何工作 MAX_PLAYERS 被定义为 26,因为这是球衣的数量
标签: c for-loop if-statement