如何使矩形的两边在c中相等? 用户输入长度和高度,程序应绘制具有特征的矩形,并在中间空出。问题是程序打印出的一面比另一面长。(注意:“x”只是我用来绘制矩形的一个符号,而不是一个变量。)

如何使矩形的两边在c中相等? 用户输入长度和高度,程序应绘制具有特征的矩形,并在中间空出。问题是程序打印出的一面比另一面长。(注意:“x”只是我用来绘制矩形的一个符号,而不是一个变量。),c,for-loop,printf,scanf,space,C,For Loop,Printf,Scanf,Space,代码如下: #include <stdio.h> #include <stdlib.h> int main() { float lenght,height,i,j; printf("insert lenght:\n"); scanf("%f",&lenght); printf("insert height:\n"); scanf("%f",&a

代码如下:

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

int main()
{
    float lenght,height,i,j;
    printf("insert lenght:\n");
    scanf("%f",&lenght);
    printf("insert height:\n");
    scanf("%f",& height);
    for (i=0; i<lenght; i++) //upper lenght
    {
        printf("x");
    }
    for (i=0; i<height; i++) //height
    {
        printf("x\n");
        for (j=0; j<lenght; j++) //space
        {
            printf(" ");
        }
        printf("x\n");
    }
    for (j=0; j<=lenght; j++) //lower lenght
    {
        printf("x");
    }
    return 0;
}
#包括
#包括
int main()
{
浮子长度,高度,i,j;
printf(“插入长度:\n”);
扫描频率(“%f”和长度);
printf(“插入高度:\n”);
扫描频率(“%f”和高度);

对于(i=0;i完美可靠的方法(注意注释):


例如,对于输入
10 5
,您的预期输出是什么?请考虑循环控件:水平空间应比宽度小2,高度也应类似。因此,对于(i=2;i使用
float
,因为循环计数器看起来不正常……您还应该从左侧边框输出
printf(“x\n”)中删除换行符
并在顶行后添加一个。@MikeCAT预期的输出是一个宽度为10、高度为5的矩形。好吧,这不是我要求的,程序要求用户输入高度和宽度,这是一个类似但不同的程序,不过谢谢你的回答。@NomeAcaso那么你打算做什么?不是一个空心矩形?我想你说得对t、 我的意思是,我有点困惑,因为你使用了一个更复杂的代码,我没有真正理解你所做的(例如,你使用的if语句),我不是很高级,我只是在两周前开始学习如何编程。@Nomeaca所以你的高度和宽度意味着正确的单词中的行和列,是的,两周就足够学习基本知识了。:)好吧,但是冷静点,伙计,我不是有意攻击你的
#include <stdio.h>

int main(void) {
    int rows, columns;
    
    printf("Input total rows and columns: ");

    if (scanf("%d%d", &rows, &columns) != 2) {
        // If the 'rows' and/or 'columns' are entered incorrectly
        printf("Please input the values correctly!\n");
        return 1;
    }

    // Iteration till 'rows'
    for (int i = 1; i <= rows; i++) {
        // Iteration till 'columns'
        for (int j = 1; j <= columns; j++)
            // Meanwhile, if 'i' is the first iteration or last iteration relative to 'rows'
            // it will print an asterisk and similarly with 'j', otherwise, a space
            if (i == 1 || i == rows || j == 1 || j == columns)
                printf("*");
            else
                printf(" ");

        // Will go to the next line on each iteration
        printf("\n");
    }

    return 0;
}
Input total rows and columns: 5 10
**********
*        *
*        *
*        *
**********

Input total rows and columns: 10 10
**********
*        *
*        *
*        *
*        *
*        *
*        *
*        *
*        *
**********