gpt4 book ai didi

c - 预览二维数组 8x8 在 c 中不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 06:18:57 24 4
gpt4 key购买 nike

我正在尝试编写显示棋盘/棋盘的代码。我给了棋盘上的每个方 block 一个数字,所以我可以手动选择棋子的位置作为 future 编程的目的。这是我得到的:

#include <stdio.h>
//{",",",",",",","},
void main()
{
int board[8][8] = {
{'1','2','3','4','5','6','7','8'},
{'9','10','11','12','13','14','15','16'},
{'17','18','19','20','21','22','23','24'},
{'25','26','27','28','29','30','31','32'},
{'33','34','35','36','37','38','39','40'},
{'41','42','43','44','45','46','47','48'},
{'49','50','51','52','53','54','55','56'},
{'57','58','59','60','61','62','63','64'}
};

/* Display the checkerboard */
printf("\n\n");
printf(" %c | %c | %c| %c | %c | %c | %c | %c \n", board[0][0], board[0][1], board[0][2], board[0][3], board[0][4], board[0][5], board[0][6], board[0][7]);
printf("---+---+---+---+---+---+---+---\n");
printf(" %c | %c | %c| %c | %c | %c | %c | %c \n", board[1][0], board[1][1], board[1][2], board[1][3], board[1][4], board[1][5], board[1][6], board[1][7]);
printf("---+---+---+---+---+---+---+---\n");
printf(" %c | %c | %c| %c | %c | %c | %c | %c \n", board[2][0], board[2][1], board[2][2], board[2][3], board[2][4], board[2][5], board[2][6], board[2][7]);
printf("---+---+---+---+---+---+---+---\n");
printf(" %c | %c | %c| %c | %c | %c | %c | %c \n", board[3][0], board[3][1], board[3][2], board[3][3], board[3][4], board[3][5], board[3][6], board[3][7]);
printf("---+---+---+---+---+---+---+---\n");
printf(" %c | %c | %c| %c | %c | %c | %c | %c \n", board[4][0], board[4][1], board[4][2], board[4][3], board[4][4], board[4][5], board[4][6], board[4][7]);
printf("---+---+---+---+---+---+---+---\n");
printf(" %c | %c | %c| %c | %c | %c | %c | %c \n", board[5][0], board[5][1], board[5][2], board[5][3], board[5][4], board[5][5], board[5][6], board[5][7]);
printf("---+---+---+---+---+---+---+---\n");
printf(" %c | %c | %c| %c | %c | %c | %c | %c \n", board[6][0], board[6][1], board[6][2], board[6][3], board[6][4], board[6][5], board[6][6], board[6][7]);
printf("---+---+---+---+---+---+---+---\n");
printf(" %c | %c | %c| %c | %c | %c | %c | %c \n", board[7][0], board[7][1], board[7][2], board[7][3], board[7][4], board[7][5], board[7][6], board[7][7]);

}

这是输出:

 1 | 2 | 3| 4 | 5 | 6 | 7 | 8                                                                                                                                                        

---+---+---+---+---+---+---+---

9 | 0 | 1| 2 | 3 | 4 | 5 | 6

---+---+---+---+---+---+---+---

7 | 8 | 9| 0 | 1 | 2 | 3 | 4

---+---+---+---+---+---+---+---

5 | 6 | 7| 8 | 9 | 0 | 1 | 2

---+---+---+---+---+---+---+---

3 | 4 | 5| 6 | 7 | 8 | 9 | 0

---+---+---+---+---+---+---+---

1 | 2 | 3| 4 | 5 | 6 | 7 | 8

---+---+---+---+---+---+---+---

9 | 0 | 1| 2 | 3 | 4 | 5 | 6

---+---+---+---+---+---+---+---

7 | 8 | 9| 0 | 1 | 2 | 3 | 4

带有如下警告:

main.c:13:37: warning: multi-character character constant[-Wmultichar]                                                                        
{'57','58','59','60','61','62','63','64'}

看起来一个字符不能打印高于 9 的字符?!我想要的是棋盘(带有二维数组)的简单预览。我试图更改此代码中的某些部分,但没有任何运气......有什么想法吗?

最佳答案

您不能将字符串放在单引号字符中。必须是唯一的字符。但是硬编码字符串中的连续整数列表是没有意义的,所以:

一个简单的带有公式的双循环更加紧凑并且节省了大量的复制/粘贴:

#include <stdio.h>
void main()
{
int i,j;

/* Display the checkerboard */
printf("\n\n");
for (i=0;i<8;i++)
{
for (j=0;j<8;j++)
{
printf("%3d ",i*8+(j+1));
}
printf("\n---+---+---+---+---+---+---+---\n");

}
}

结果:

  1   2   3   4   5   6   7   8
---+---+---+---+---+---+---+---
9 10 11 12 13 14 15 16
---+---+---+---+---+---+---+---
17 18 19 20 21 22 23 24
---+---+---+---+---+---+---+---
25 26 27 28 29 30 31 32
---+---+---+---+---+---+---+---
33 34 35 36 37 38 39 40
---+---+---+---+---+---+---+---
41 42 43 44 45 46 47 48
---+---+---+---+---+---+---+---
49 50 51 52 53 54 55 56
---+---+---+---+---+---+---+---
57 58 59 60 61 62 63 64
---+---+---+---+---+---+---+---

具有更好框架的增强版(只是为了好玩)

#include <stdio.h>
void main()
{
int i,j;

/* Display the checkerboard */
printf("\n\n\n|---+---+---+---+---+---+---+---+\n");

for (i=0;i<8;i++)
{
printf("|");
for (j=0;j<8;j++)
{
printf("%2d |",i*8+(j+1));
}
printf("\n|---+---+---+---+---+---+---+---+\n");

}

结果

|---+---+---+---+---+---+---+---+
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|---+---+---+---+---+---+---+---+
| 9 |10 |11 |12 |13 |14 |15 |16 |
|---+---+---+---+---+---+---+---+
|17 |18 |19 |20 |21 |22 |23 |24 |
|---+---+---+---+---+---+---+---+
|25 |26 |27 |28 |29 |30 |31 |32 |
|---+---+---+---+---+---+---+---+
|33 |34 |35 |36 |37 |38 |39 |40 |
|---+---+---+---+---+---+---+---+
|41 |42 |43 |44 |45 |46 |47 |48 |
|---+---+---+---+---+---+---+---+
|49 |50 |51 |52 |53 |54 |55 |56 |
|---+---+---+---+---+---+---+---+
|57 |58 |59 |60 |61 |62 |63 |64 |
|---+---+---+---+---+---+---+---+

仍然不是精通 ASCII 艺术,但更好:)

关于c - 预览二维数组 8x8 在 c 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39656636/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com