[英]Nested Loop for C Programming
I have some problem about Nested Loop on C programming I tried to print the list like this: 我对C编程上的嵌套循环有一些问题,我尝试打印如下列表:
| |0|1|2|3|4|5|6|7|8|9|
|0| | | | | | | | | | |
|1| | | | | | | | | | |
|2| | | | | | | | | | |
|3| | | | | | | | | | |
|4| | | | | | | | | | |
|5| | | | | | | | | | |
|6| | | | | | | | | | |
|7| | | | | | | | | | |
|8| | | | | | | | | | |
|9| | | | | | | | | | |
but there are something problem when i type my code and display: 但是当我键入代码并显示时出现问题:
| |0|1|2|3|4|5|6|7|8|9|
|0|0|0|0|0|0|0|0|0|0|0|
|1|0|1|1|1|1|1|1|1|1|1|
|2|0|2|2|2|2|2|2|2|2|2|
|3|0|3|3|3|3|3|3|3|3|3|
|4|0|4|4|4|4|4|4|4|4|4|
|5|0|5|5|5|5|5|5|5|5|5|
|6|0|6|6|6|6|6|6|6|6|6|
|7|0|7|7|7|7|7|7|7|7|7|
|8|0|8|8|8|8|8|8|8|8|8|
|9|0|9|9|9|9|9|9|9|9|9|
There is my code : 有我的代码:
void displayBoard(Cell board[BOARD_HEIGHT][BOARD_WIDTH], Player * player)
{
int i, j;
printf("| ");
for (j = 0; j < BOARD_WIDTH; j++)
{
printf("|%d",j);
}
printf("|\n");
for (i = 0; i < BOARD_HEIGHT; i++)
{
for (j = 0; j < BOARD_WIDTH; j++)
{
printf("|%d",i);
if (j == 0)
{
printf("|%d",j);
}
}
printf("|\n");
}
printf("\n");
}
Have someone can help for this condition: only one row and one column, other is empty. 有个人可以帮助您解决这种情况:只有一行一行,其他为空。
At no point in the body of the inner loop are you printing spaces. 在内循环主体中的任何位置都没有打印空格。 You're instead printing the value of
i
, which is the column number. 相反,您要打印
i
的值( i
列号)。
printf("|%d",i);
if (j == 0)
{
printf("|%d",j);
}
Instead, print i
only on the first iteration and print the space each time: 相反,只在第一次迭代中打印
i
,然后每次打印空格:
if (j == 0) {
printf("|%d",i);
}
printf("| ");
Output: 输出:
| |0|1|2|3|4|5|6|7|8|
|0| | | | | | | | | |
|1| | | | | | | | | |
|2| | | | | | | | | |
|3| | | | | | | | | |
|4| | | | | | | | | |
|5| | | | | | | | | |
|6| | | | | | | | | |
|7| | | | | | | | | |
|8| | | | | | | | | |
The key to getting this done correct is enclosing the repeating logic (the blank cells) in the loop while confining the specialized logic to be outside the loop: 正确完成此操作的关键是将重复逻辑(空白单元格)包含在循环中,同时将专用逻辑限制在循环之外:
void displayBoard(int height, int width)
{
int i, j;
printf("| ");
for (j = 0; j < width; j++) {
printf("|%d", j);
}
printf("|\n");
for (i = 0; i < height; i++) {
printf("|%d", i);
for (j = 0; j < width; j++) {
printf("| ");
}
printf("|\n");
}
}
Look mom! 看妈妈! no
if
s 否,
if
This is how you should code it: 这是应该如何编码:
void displayBoard(Cell board[BOARD_HEIGHT][BOARD_WIDTH], Player * player)
{
int i, j;
printf("| ");
for (j = 0; j < BOARD_WIDTH; j++)
{
printf("|%d",j);
}
printf("|\n");
for (i = 0; i < BOARD_HEIGHT; i++)
{
for (j = 0; j < BOARD_WIDTH; j++)
{
if (j == 0)
{
printf("|%d",i);
}
printf("| ");
}
printf("|\n");
}
printf("\n");
}
There are couple of issues with your current code: 您目前的程式码有几个问题:
You printf("|%d",i);
您
printf("|%d",i);
in the inner loop for every j
while what you really want is just to print it when j == 0
once. 在每个
j
的内循环中,而您真正想要的只是在j == 0
时打印一次。 The rests, you want to printf("| ")
: 剩下的,你要
printf("| ")
:
printf("| "); //you want to make this blank if (j == 0) { printf("|%d",i); //you want to print i here, not j }
between the print of number when j == 0
and the print of blank should be reversed: 在
j == 0
时打印数字与空白打印之间应该反转:
if (j == 0) { printf("|%d",i); //you want to print i here, not j } printf("| "); //put the blank printing after the number
You are printing the number i in every line, when you want a blank or the array data. 当您需要空白或数组数据时,将在每行中打印数字i。 you also need to get rid of printing a zero every row, which is what you have coded in. Here is that part of the code corrected:
您还需要避免每行打印零,这是您编码的内容。这是已纠正的部分代码:
for (i = 0; i < BOARD_HEIGHT; i++)
{
for (j = 0; j < BOARD_WIDTH; j++)
{
if (j == 0)
{
printf("|%d",i);
}
printf("| "); ///or printf("|%d",board[i][j]) if you are after stored data
}
printf("|\n");
}
hope this helps. 希望这可以帮助。
Try this: 尝试这个:
#define BOARD_HEIGHT 10
#define BOARD_WIDTH 10
void displayBoard()
{
int i, j;
printf("| ");
for (j = 0; j < BOARD_WIDTH; j++)
{
printf("|%d", j);
}
printf("|\n");
for (i = 0; i < BOARD_HEIGHT; i++)
{
printf("|%d", i);
for (j = 1; j < BOARD_WIDTH; j++)
{
printf("| ");
}
printf("| |\n");
}
}
BTW, there is no need to pass any arguments to this function. 顺便说一句,不需要将任何参数传递给此函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.