使用while循环打印X

时间:2020-06-20 11:41:55

标签: c while-loop

我想编写一个程序,该程序从标准输入中读取一个整数n,并以“ X”形打印一个nxn模式的星号和破折号。

N必须为奇数且N> = 5。

我的代码

#include <stdio.h>

int main(void)  {
    
    //making sure they enter an odd number >= 5 
    
    int endloop = 0; 
    
    int size;
    while (endloop == 0) { 
        printf("Enter size:");
        scanf("%d", &size);
        if (size % 2 == 0 || size < 5) {
            printf("Invalid, try again\n");
        } else {
            endloop = 1;
        } 
    }
    
   
    int downwards = 0;
    while (downwards < size) {
    
        int across = 0; 
        while (across < size) {
            if (downwards == across) {
                printf("*");
            }
            if (across == size - downwards - 1) {
                printf("*");
            } else {
                printf("-");
            }
            across++; 
        }
        downwards++;
        printf("\n");
    }
    
    return 0; 
} 

结果:

Enter size:5
*----*
-*--*-
--**--
-*-*--
*---*-

应该看起来像这样:

*---*
-*-*-
--*--
-*-*-
*---*

我不知道我做错了什么。即使我在中指出,跨行也将打印6个字符而不是5个字符

0 个答案:

没有答案