这是我到目前为止写的:
int main(void) {
int playerJerseys[5];
int playerRatings[5];
printf("Enter player 1's jersey number:\n");
scanf("%d",&playerJerseys[0]);
printf("Enter player 1's rating:\n\n");
scanf("%d",&playerRatings[0]);
printf("Enter player 2's jersey number:\n");
scanf("%d",&playerJerseys[1]);
printf("Enter player 2's rating:\n\n");
scanf("%d",&playerRatings[1]);
printf("Enter player 3's jersey number:\n");
scanf("%d",&playerJerseys[2]);
printf("Enter player 3's rating:\n\n");
scanf("%d",&playerRatings[2]);
printf("Enter player 4's jersey number:\n");
scanf("%d",&playerJerseys[3]);
printf("Enter player 4's rating:\n\n");
scanf("%d",&playerRatings[3]);
printf("Enter player 5's jersey number:\n");
scanf("%d",&playerJerseys[4]);
printf("Enter player 5's rating:\n\n");
scanf("%d",&playerRatings[4]);
printf("ROSTER\n");
printf("Player 1 -- Jersey number: %d, Rating: %d\n",playerJerseys[0],playerRatings[0]);
printf("Player 2 -- Jersey number: %d, Rating: %d\n",playerJerseys[1],playerRatings[1]);
printf("Player 3 -- Jersey number: %d, Rating: %d\n",playerJerseys[2],playerRatings[2]);
printf("Player 4 -- Jersey number: %d, Rating: %d\n",playerJerseys[3],playerRatings[3]);
printf("Player 5 -- Jersey number: %d, Rating: %d\n\n",playerJerseys[4],playerRatings[4]);
char selection;
printf("MENU\n");
printf("u - Update player rating\n");
printf("a - Output players above a rating\n");
printf("r - Replace player\n");
printf("o - Output roster\n");
printf("q - Quit\n\n");
printf("Choose an option:\n");
scanf("%c",&selection);
if(selection == 'q'){
return 0;
}
else if(selection == 'u'){
int userJersey;
int userRating;
printf("Enter a jersey number:\n");
scanf("%d",&userJersey);
for(int i = 0; i < 5; ++i){
if(playerJerseys[i] == userJersey){
printf("Enter a new rating for player:\n");
scanf("%d",&userRating);
playerRatings[i] = userRating;
}
}
}
else if(selection == 'o'){
printf("ROSTER\n");
printf("Player 1 -- Jersey number: %d, Rating: %d\n",playerJerseys[0],playerRatings[0]);
printf("Player 2 -- Jersey number: %d, Rating: %d\n",playerJerseys[1],playerRatings[1]);
printf("Player 3 -- Jersey number: %d, Rating: %d\n",playerJerseys[2],playerRatings[2]);
printf("Player 4 -- Jersey number: %d, Rating: %d\n",playerJerseys[3],playerRatings[3]);
printf("Player 5 -- Jersey number: %d, Rating: %d\n\n",playerJerseys[4],playerRatings[4]);
}
return 0;
}
提供此输入时:
84 7
23 4
4 5
30 2
66 9
u
4
6
o
q
这是输出:
Player 1 -- Jersey number: 84, Rating: 7
Player 2 -- Jersey number: 23, Rating: 4
Player 3 -- Jersey number: 4, Rating: 5
Player 4 -- Jersey number: 30, Rating: 2
Player 5 -- Jersey number: 66, Rating: 9
MENU
u - Update player rating
a - Output players above a rating
r - Replace player
o - Output roster
q - Quit
Choose an option:
所需的输出是:
ROSTER
Player 1 -- Jersey number: 84, Rating: 7
Player 2 -- Jersey number: 23, Rating: 4
Player 3 -- Jersey number: 4, Rating: 6
Player 4 -- Jersey number: 30, Rating: 2
Player 5 -- Jersey number: 66, Rating: 9
MENU
u - Update player rating
a - Output players above a rating
r - Replace player
o - Output roster
q - Quit
Choose an option:
当提供u作为输入时,随后的数字应该会更改其中一位玩家的评分,但对我而言,它不起作用。我不确定是否只是不知道如何比较字符?还是我的if陈述和else-if陈述设置不正确?任何建议表示赞赏。
答案 0 :(得分:2)
尝试在%c之前留出空格,这是可行的。 喜欢
scanf(" %c", &selection);