简体   繁体   English

我如何将它打印在同一行上

[英]How would i print this on the same line

I am trying to write a program that builds a "building" using multiple characters.我正在尝试编写一个使用多个字符构建“建筑物”的程序。 There are 3 functions:有3个功能:

  1. int ValidateData(int low, int high, char type)

This function validates that the user's entry meets the required parameters and returns the correct number depending on the type parameter.此函数验证用户输入是否满足所需参数,并根据type参数返回正确的数字。

No problems here.这里没有问题。

  1. int drawUpperFloors(int numOfWindows)

This function is supposed to draw the number of floors depending on how many windows the user entered.这个函数应该根据用户输入的窗口数量来绘制楼层数。 My problem is that I cannot seem to get the windows to draw on the same line.我的问题是我似乎无法让窗户在同一条线上绘制。 If the user entered 3 the output should be: (Took awhile to get that right)如果用户输入 3,输出应该是:(花了一段时间才弄对)

+---------------+ | | | === === === | | | | | | | | | | === === === | | |

Instead I get a window on a new line until the loop reaches its limit ( numOfWindows ), so if numOfWindows = 20 then I get 20 windows each on a different line.相反,我在新行上得到一个窗口,直到循环达到其限制( numOfWindows ),所以如果numOfWindows = 20 那么我在不同的行上得到 20 个窗口。 I would appreciate insight into how I might do this.我希望能深入了解我如何做到这一点。

  1. void DrawGroundFloor(int numOfWindows)

    This function is almost identical to the drawUpperFloors( ) function except that the first window will be replaced with a door.该函数几乎与drawUpperFloors( )函数相同,只是第一个窗口将被替换为一扇门。 So, if the numOfWindows parameter is 2, then the ground floor will consist of 1 door and 1 window.因此,如果 numOfWindows 参数为 2,则底层将由 1 个门和 1 个窗口组成。 If the number of windows is only 1, then only a door will be drawn.如果窗口数仅为 1,则只会绘制一扇门。

    Please take note of the following details: a).请注意以下细节:a)。 Each window (and door) is exactly 3 columns wide.每个窗户(和门)正好是 3 列宽。

    b). b)。 The bottom of the floor is drawn using the * character.地板的底部是使用 * 字符绘制的。

    An example of a ground floor with 5 windows woulb be:一楼有 5 个窗户的例子是:

+-----------------------+ | | | === === === === === | | | | | | | | | | | | | | | | === === === === | | | | | *************************

Considering I have been unable to do the second function this comes as no surprise.考虑到我一直无法执行第二个功能,这不足为奇。

I have tried using up to 3 loops using if and else , but I still cannot, no matter what I try (even modulus) get it print in the same line.我已经尝试使用ifelse最多使用 3 个循环,但我仍然不能,无论我尝试什么(偶数模数)都将它打印在同一行中。 Any input would be appreciated.任何输入将不胜感激。

EDIT here's the code for the 2 functions so far编辑这里是到目前为止 2 个函数的代码

int GetValidData(int low, int high, char type)
{
    int number;
    int number2;

    int i=1;
    int j;
    if (type == 'F')
    {
        printf("Enter the amount of floors you would like(1-1000)\n");
        scanf("%d", &number);
        fflush(stdin);


        if (number < 1 || number>1000)
        {
            while(i!=0)
            {
                printf("You have entered an invalid floor number\nPlease enter a valid number: ");

                scanf("%d", &number);
                fflush(stdin);
                if (number >= 1 && number <= 1000)
                {
                    printf("Thank you!\n");
                    i = 0;

                }
            }

        }
        return number;
    }



    printf("Enter the amount of windows you would like(1-20)\n");
    scanf("%d", &number);


    if (type == 'W')
    {
        if (number < 1 || number>20)
        {
            while(i!=0)
            {
                printf("You have entered an invalid number\nPlease enter a valid number: ");
                scanf("%d", &number);
                if (number >= 1 && number <= 20)
                {
                    printf("Thank You!\n");
                    i = 0;
                }
            }

        }

        return number;




int drawUpperFloors(int numOfWindows)
{
    int check = numOfWindows;
    int i;
    int j;
    for (i = 0; i <= numOfWindows;i++)
    {
        printf("===\n");
        for (j = 0; j <= numOfWindows;j++)
        {
            printf("|  |\n");
        }

        printf("===");
    }

} }

My problem is that I cannot seem to get the windows to draw on the same line.我的问题是我似乎无法让窗户在同一条线上绘制。

The basic problem is you're adding newlines to your print statements when drawing an individual window.基本问题是您在绘制单个窗口时向打印语句添加换行符。

for (i = 0; i <= numOfWindows;i++)
{
    /* There shouldn't be a newline here */
    printf("===\n");
    for (j = 0; j <= numOfWindows;j++)
    {
        /* Nor a newline here */
        printf("|  |\n");
    }

    printf("===");
}

You're also counting your windows from 0 which results in one too many windows.您还从 0 开始计算您的窗口,这会导致一个窗口过多。 Either start from 1 or go until i < numOfWindows .要么从 1 开始,要么一直到i < numOfWindows

You have to draw each line in its own loop, including the outer walls, ending with a newline.您必须在自己的循环中绘制每条线,包括外墙,并以换行符结尾。 First the upper sills, then the windows, then the lower sills.首先是上门槛,然后是窗户,然后是下门槛。

void drawUpperFloors(int numOfWindows)
{
    int i;

    /* Draw the upper sills, including the outer walls */
    printf("|  ");
    for (i = 1; i <= numOfWindows;i++)
    {
        printf("=== ");
    }
    printf(" |\n");

    /* Draw the windows, including the outer walls */
    printf("|  ");
    for (i = 1; i <= numOfWindows;i++)
    {
        printf("| | ");
    }
    printf(" |\n");

    /* Draw the lower sills, including the outer walls */
    printf("|  ");
    for (i = 1; i <= numOfWindows;i++)
    {
        printf("=== ");
    }
    printf(" |\n");
}

That gets repetitive, so it's best to put drawing a line of a floor into a function.这会变得重复,因此最好将绘制一条地板线放入一个函数中。

void drawFloorLine(int num, const char *to_repeat) {
    int i;

    printf("|  ");
    for (i = 1; i <= num;i++)
    {
        printf("%s", to_repeat);
    }
    printf(" |\n");
}

void drawUpperFloors(int numOfWindows)
{
    drawFloorLine(numOfWindows, "=== ");    
    drawFloorLine(numOfWindows, "| | ");
    drawFloorLine(numOfWindows, "=== ");
}

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

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