繁体   English   中英

使用垂体,扩展的ASCII字符绘制用于用户定义尺寸的矩形

[英]drawing a rectangle for user-defined dimensions using for lops, using extended ASCII characters

我做了一部分,但似乎无法获得整个矩形。 有人可以指出我做错了吗?

这是我的代码:

printf("Enter the length and width of the rectangle : ");
scanf("%d%d",&length,&width);


printf("\n%c", 218);
for(i=1;i<=length;i++)
{
        printf("%c",196);

}

printf("%c",191);


 for(j=1;j<=width;j++)
 {
     printf("\n");
     printf("%c",179);
 }
    printf("\n");
    printf("%c", 192);

     for(i=1;i<=length;i++)
{
        printf("%c",196);

}

printf("%c", 217);

return 0;

我的输出

您仅打印第一列,而不打印第二列。 一种方法是在整个矩形上循环并检查您是否在边缘。

#include <stdio.h>

int main()
{
    int length, width, i, j;

    printf("Enter the length and width of the rectangle : ");
    scanf("%d%d",&length, &width);

    for(i=0;i<width;i++)
    {
        for(j=0;j<length;j++)
        {
            if( j==0 || j==length-1 || i==0 || i==width-1 )
            {
                printf("%c", '*');
            }
            else
            {
                printf(" ");
            }
        }
        printf("\n");
    }

    return 0;
}

在您的中间循环中,您只需要打印一个垂直条,然后再转到下一行。

您需要打印一次条形,然后添加另一个循环以打印空格,然后再打印一个条形:

  for(j=1;j<=width;j++)
  {
     printf("\n");
     printf("%c",179);
     for (i=1;i<=length;i++) {
       printf(" ");
     }
     printf("%c",179);
  }
#include <stdio.h>
#include <stdlib.h>

int main()
{
int length, width,i,j;
   printf("Enter the length and width of the rectangle : ");
   scanf("%d%d",&length,&width);


printf("\n%c", 218);
for(i=1;i<=length;i++)
{
    printf("%c",196);

 }

   printf("%c",191);


    for(j=1;j<=width;j++)
 {
 printf("\n");
 printf("%c",179);
 for (i=1;i<=length;i++) {
   printf(" ");
 }
  printf("%c",179);
  }
    printf("\n");
    printf("%c", 192);

  for(i=1;i<=length;i++)
   { 
    printf("%c",196);

    }

 printf("%c", 217);

  return 0;
 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM