gpt4 book ai didi

c++ - 如何用C语言制作棋盘?

转载 作者:行者123 更新时间:2023-11-30 21:00:36 24 4
gpt4 key购买 nike

我是 C 语言新手,我正在尝试编写一个可以输出棋盘图案的程序。但是,我不明白我的下一步要做什么。谁能帮我解决这个问题吗?

主要:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

int main() {
int length,width;
enum colors{Black,White};

printf("Enter the length:\n");
scanf("%d",length);
printf("Enter the width:\n");
scanf("%d",width);

int board[length][width];
createBoard(length,width);
for(int i =0;i< sizeof(board);i++) {

if(i%2==0) {
// To print black
printf("[X]");
}
else {
// To print white
printf("[ ]");
}
}
} //main

CreateBoard函数:

int createBoard(int len, int wid){
bool b = false;
for (int i = 0; i < len; i++ ) {
for (int j = 0; j < wid; j++ ) {
// To alternate between black and white
if(b = false) {
board[i][j] = board[White][Black];
b = false;
}
else {
board[i][j] = board[Black][White];
b = true;
}
}
}
return board;
}

最佳答案

首先学习如何使用scanf()。

int length;

不正确:scanf("%d",length);正确:scanf("%d",&length);

希望这有帮助:

#include <stdio.h>

int main(void)
{
int length,width,i,j;
printf("Enter the length:\n");
scanf("%d",&length);
printf("Enter the width:\n");
scanf("%d",&width);
for(i=1;i<=length;i++)
{
for(j=1;j<=width;j++)
{
if((i+j)%2==0)
printf("[X]");
else printf("[ ]");
}
printf("\n");
}
return 0;
}

注意:确保长度和宽度均匀。

关于c++ - 如何用C语言制作棋盘?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40252555/

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