我想打印下面的图案,用户输入n和m(4和6),外边界应该用星星组成,里面应该用圆圈填充。
****** ****** ****** ******
*oooo* *oooo* *oooo* *oooo*
*oooo* *oooo* *oooo* *oooo*
*oooo* *oooo* *oooo* *oooo*
*oooo* *oooo* *oooo* *oooo*
****** ****** ****** ******
****** ****** ****** ******
*oooo* *oooo* *oooo* *oooo*
*oooo* *oooo* *oooo* *oooo*
*oooo* *oooo* *oooo* *oooo*
*oooo* *oooo* *oooo* *oooo*
****** ****** ****** ******
****** ****** ****** ******
*oooo* *oooo* *oooo* *oooo*
*oooo* *oooo* *oooo* *oooo*
*oooo* *oooo* *oooo* *oooo*
*oooo* *oooo* *oooo* *oooo*
****** ****** ****** ******
****** ****** ****** ******
*oooo* *oooo* *oooo* *oooo*
*oooo* *oooo* *oooo* *oooo*
*oooo* *oooo* *oooo* *oooo*
*oooo* *oooo* *oooo* *oooo*
****** ****** ****** ******
我写的代码如下。我做错了什么?
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int n,i,j,k,m,n1;
printf("enter table size and enter square size");
scanf("%d %d", &n, &n1);
for (i = 0; i < n; i++) {
for (j = 0; j < n1; j++)
{
for ( k = 0; k < n; k++)
{
for ( m = 0; m < n1; m++)
{
{
if(i==0 || i==n-1)
{
printf("*");
}
else if(j==0 || j==n-1)
{
printf("*");
}
else
{
printf("o");
}
}
}
printf(" ");
}
printf("\n");
}
printf("\n");
}
system("pause");
return 0;
}
答案 0 :(得分:1)
更正的代码是
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int n,i,j,k,m,n1;
printf("enter table size and enter square size");
scanf("%d %d", &n, &n1);
for (i = 0; i < n; i++) {
for (j = 0; j < n1; j++)
{
for ( k = 0; k < n; k++)
{
for ( m = 0; m < n1; m++)
{
{
if(j==0 || j==n1-1)
{
printf("*");
}
else if(m==0 || m==n1-1)
{
printf("*");
}
else
{
printf("o");
}
}
}
printf(" ");
}
printf("\n");
}
printf("\n");
}
system("pause");
return 0;
}
您的索引变量执行以下操作:
|----------------n----------------------|
|--m--|
- ******* ******* ******* ******* *******
| *o ...
| *o ...
j *o ...
|
|
-
所以基本上你必须在每个正方形的第一行或最后一行或当m在正方形的第一行或最后一行(n1)时打印*。
希望这有帮助