0

我需要制作一个如下所示的程序:

Player name 1:   <input> 
Player name 2:   <input> 
<output> <output> 
(Player 1's)Score1: <input1>
            Score2: <input2> 
(player 2's)Score1: <input1>
            Score2: <input2>

(player 1's)<output1>
            <output2> 
(player 2's)<output1>
            <output2>

或准确地说:

Number     Player Name     Score
                           Game1     Game2
------     -----------    -------   -------
 [1]         <name1>      <score1>  <score2> 
 [2]         <name2>      <score1>  <score2>

我需要做一个循环来指示名称旁边的数字,但我不知道该怎么做。

这是我的代码:

int main()

{
    int x=1;
    char player[PLAYERS][LENGTH] = {"-----"};
    char scorex[GAME][LENGTH] = {"0.00"};

    int i,j;//COUNTERS

    for (i=0; i<PLAYERS; i++)
    {
        printf("Player Name %d:\t",x);
        fgets(player[i], LENGTH, stdin);
        x++;
    }   

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

    for (x=1; x<=PLAYERS; x++)
    {
        printf("score %d:\t", x);
        for (i=0 ;i<GAME; i++)
        {
        fgets(scorex[i], LENGTH, stdin);
        }
        printf("%5s\n", scorex[i]);
    }
    return 0;
}

我可以用循环做什么?帮助?

4

2 回答 2

0

To answer using the same flavor of coding style, you can nest for loops as:

    for (i=0;i<PLAYERS;i++)
    {
        printf("%10s\n", player[i]);

        for (x=1; x<=PLAYERS; x++)
        {
            printf("score %d:\t", x);
            for (j=0 ;j<GAME; j++)      // << notice J not I
            {
               fgets(scorex[j], LENGTH, stdin);
            } // end for j = 0

            printf("%5s\n", scorex[i]);

        }  // end for x=1

    }  // end for i = 0

Note. I didn't debug your code, just wrote nested for loops, you will need to do more work. One of the bugs might have been in using i for an inner loop and an outer loop.

于 2013-09-05T16:15:25.620 回答
0

只是解决输出部分,下面的代码是做你所描述的一种方法:(格式化需要一些工作)

#include <windows.h>
#include <ansi_c.h>

enum    {
    name1,
    name2,
    name3,
    name_max
};

char *name[name_max]={"name1","name2","name3"};
char *score1[name_max]={"12","11","1"};
char *score2[name_max]={"1","13","22"};


int main(void)
{
    int line;

    printf("Number\tPlayer Name\tScore\n");
    printf("\t\tGame1\tGame 2\n");

    for (line=name1;line < name_max;line++)
    {
        printf("%d\t%s\t%s\t%s\n", line+1, name[line], score1[line], score2[line]);
    }

    getchar();
    return 0;   
}

结果如下:

在此处输入图像描述

于 2013-09-05T14:59:26.623 回答