到目前爲止,我可以使此代碼工作的唯一方法是使用switch語句。有沒有一種方法可以帶走這個開關語句並創建一個數組。我聽說我可以這樣做:char name[11]= {"name 1", "name 2"};
等等,但我不知道如何在程序中打印該內容。因爲對於我的數字,我只是將它分配給一個非數組變量並使用printf來打印它。如何在C中創建一個字符串數組?
我的代碼:
#include <stdio.h>
int main(){
int i;
int player [11] = {1, 2, 10, 13, 21, 22, 24, 25, 31,32, 33};
int points [11] = {60, 297, 11, 373, 154, 52, 555, 218, 29, 242, 257};
int games [11] = {33, 35, 12, 35, 35, 35, 35, 35, 22,35, 35};
int bestplayer = 0;
float bestppg = 0.0;
float ppg [11] ;
for (i=0; i<11; i++){
ppg[i] = (float)points [i]/(float)games [i] ;
printf("%d \t %d \t %d \t %.1f ppg\n", player[i], games[i], points[i],ppg[i]);
if (ppg[i]>bestplayer){
bestplayer = player[i];
bestppg = ppg[i];
}
}
printf("\nThe player with the most points per game is #%d ", bestplayer);
switch(bestplayer){
case 1:
printf("Player 1");
break;
case 2:
printf("Player 2");
break;
case 10:
printf("Player 3");
break;
case 13:
printf("Player 4");
break;
case 21:
printf("Player 5");
break;
case 22:
printf("Player 6");
break;
case 24:
printf("Player 7");
break;
case 25:
printf("Player 8");
break;
case 31:
printf("Player 9");
break;
case 32:
printf("Player 10");
break;
case 33:
printf("Player 11");
break;
default:
printf("Invalid Player");
break;
}
printf(" with %.1f ppg.\n",bestppg);
return 0;
}
字符[10] [100] -10每個具有100個字符串字符,最好有char **,並根據需要分配內存 – radar 2014-10-11 01:07:00