我需要制作一个如下所示的程序:
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;
}
我可以用循环做什么? 帮忙?
最佳答案
只是解决输出部分,以下代码是执行您所描述的操作的一种方法:(格式化需要一些工作)
#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;
}
结果如下:
关于c - 如何在一个循环内完美地创建一个循环,同时又在另一个循环内创建一个循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18638990/