2014-10-11 220 views
0

到目前爲止,我可以使此代碼工作的唯一方法是使用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; 
} 
+0

字符[10] [100] -10每個具有100個字符串字符,最好有char **,並根據需要分配內存 – radar 2014-10-11 01:07:00

回答

1

在當前結構中使用char*數組的主要問題是,您不會跟蹤索引的最佳玩家。如果你這樣做,那麼你可以創建一個數組和索引。

#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}; 
    const char* names[11] = { 
     "Jaylon Tate","Joseph Bertrand","Jaylon Tate","Tracy Abrams","Malcolm Hill","Maverick Morgan","Rayvonte Rice","Kendrick Nunn","Austin Colbert","Nnanna Egwu","Jon Ekey" 
    }; 
    int bestplayer = 0; 
    float bestppg = 0.0; 
    float ppg [11] ; 
    int bestIndex = 0; 
    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]; 
      bestIndex = i; 
     } 
    } 

    printf("\nThe player with the most points per game is #%d %s with %.1f ppg.\n", bestplayer, names[bestIndex], bestppg); 
    return 0; 
} 
+0

'names'的第二個'const'不會出錯。 (使其他數組''const作爲exerise離開)。 – Deduplicator 2014-10-11 01:12:42

0

像這樣:

int i; 
char *names[5] = { "name1", "name2", "name3", "name4", "name5" }; 

for (i = 0; i<5; i++) 
{ 
    printf("%s\n", names[i]); 
} 

一兩件事。聲明

char name[11]; 

聲明單個名稱的空間長度爲10個字符,末尾爲零終止符,而不是您想要的數組。

+1

更好地給它一些'const'限定符。 – Deduplicator 2014-10-11 01:11:18

0

避免你通過記住最好播放器的索引,用它作爲一個指標爲字符串常量陣列switch

#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] ; 

    int best = 0; 
    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]; 
     best = i; 
    } 
    } 

    const char* const players[] = { "Jaylon Tate", "Joseph Bertrand", "Jaylon Tate", "Tracy Abrams", "Malcolm Hill", "Maverick Morgan", "Rayvonte Rice", "Kendrick Nunn", "Austin Colbert", "Nnanna Egwu", "Jon Ekey" }; 

    printf("\nThe player with the most points per game is %s with %.1f ppg.\n", players[best], bestppg); 
} 
相關問題