[英]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.