【发布时间】:2025-12-29 04:10:12
【问题描述】:
我有一个在 main 函数之外有一个 for 循环的程序:
#include <stdio.h>
void inputMaze(char maze[], int maxX, int maxY);
int main()
{
//Number of columns
int maxX= 0;
//Number of rows
int maxY= 0;
printf("Number of rows? ");
scanf("%d", &maxY);
printf("Number of columns? ");
scanf("%d", &maxX);
if(maxX*maxY>300){
printf("Number of cells exceeds maximum!/n");
}
char maze[maxX][maxY];
inputMaze(maze,maxX, maxY);
return 0;
}
void inputMaze(char maze[], int maxX, int maxY){
int i;
for(i=0; i<maxY; i=i+1){
printf("Input row %d ", i);
scanf(" %c", &maze[i]);
}
}
输出给了我这个:
Number of rows? 10
Number of columns? 10
Input row 0 S#####
Input row 1 Input row 2 Input row 3 Input row 4 Input row 5 Input row 6 D.....
Input row 7 Input row 8 Input row 9
Process returned 0 (0x0) execution time : 11.526 s
Press any key to continue.
我不希望 Input row 1 Input row 2.... 像这样打印。我正在尝试获取它,以便每次在新行上打印输入行 i 并且用户可以输入新行。我认为问题可能与 scanf 存储到二维数组有关。我希望一次写入迷宫数组中的一行,然后该行中的每个元素被一个字母占用,但我似乎无法做到这一点。
【问题讨论】:
标签: arrays for-loop multidimensional-array scanf