gpt4 book ai didi

c++ - 代码在调试器中有效,但在可执行程序中无效

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

我在 Win7 x64 上的 4.7.2 上使用 MinGW GCC

这是练习:

http://postimg.org/image/v4xnpcxc3/

这是我未完成的代码(没有有效的退出循环条件)不起作用(字母永远不会在一行或一列中出现。我尝试调试程序并且在调试器代码中工作??!!):

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

int main(void)
{
char array[10][10];
int direction = 0;
int i = 0, j = 0, cnt = 1;
for(int i = 0; i < 10; i++)
{
for(int j = 0; j < 10; j++)
{
array[i][j] = '*';
}
}
int z = 200;
array[i][j] = 'A';
while(z-- > 0)
{
srand((unsigned)time(NULL));
direction = rand() % 4;
switch(direction)
{
case 0:
if(i != 0)
i--;
break;
case 1:
if(j != 9)
j++;
break;
case 2:
if(i != 9)
i++;
break;
case 3:
if(j != 0)
j--;
break;
}
if(array[i][j] == '*')
{
array[i][j] = 'A' + cnt;
cnt++;
}
if(cnt == 26)
break;
}
for(int i = 0; i < 10; i++)
{
for(int j = 0; j < 10; j++)
{
printf("%c ", array[i][j]);
}
printf("\n");
}
}

最佳答案

首先

  array[i][j] = 'A';

实际上与 array[0][0] = 'A'; 相同:

for(int i = 0; i < 10; i++)

for(int j = 0; j < 10; j++)

隐藏 ij 之前的声明:

int i = 0, j = 0, cnt = 1;

其次,在循环中 rand 之前调用 srand 是不好的。仅在程序开始时调用一次 srand

关于c++ - 代码在调试器中有效,但在可执行程序中无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17902173/

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