循环不在C中执行

时间:2017-11-01 03:31:10

标签: c

我无法让这个程序用标签打印出符号行。我列出了应该如何打印的图片。目前它正在工作,除了没有缩进。任何帮助都将非常感激。

这个想法是显示带有缩进('\ t')的偶数行和没有缩进的奇数行:

正确输出的示例

#include <stdio.h>

int main(void) {
    int num_lines;
    printf("Enter a number of lines, greater than or equal to 7, to print :  ");
    scanf("%d", &num_lines);

    if (num_lines < 7) {
        while ( num_lines < 7 ) {
            printf("Enter the number of lines to print\nMust be greater than or equal to 7 :  ");
            scanf("%d", &num_lines);
        }
    }

    char symbol;
    printf("Choose a symbol/character to be displayed */&/+/0/x :  ");
    scanf(" %c", &symbol);

    int num_symbols;
    printf("Enter the number of symbols to print per line :  ");
    scanf("%d", &num_symbols);

    if (num_symbols < 7 || num_symbols > 27) {
        num_symbols = 19;
    }

    while (num_lines > 0) {
        int n = num_symbols;
        int nl = 1;
        int nll = nl / 2;

        while (nl <= num_lines) {
            if ( (nl % 2) == 0) {
                while (nll > 0) {
                    printf("\t");
                    --nll;
                }
                while (n > 0) {
                    printf("%c", symbol);
                    --n;
                }
            }
            else {
                while (n > 0) {
                    printf("%c", symbol);
                    --n;
                }
            }
            ++nl;
        }
        printf("\n");
        --num_lines;
    }
    return;
}

3 个答案:

答案 0 :(得分:0)

您可以尝试打印所需的图案:

#include<stdio.h>

int main(void) {
    int num_lines,i,j;
    printf("Enter a number of lines, greater than or equal to 7, to print :  ");
    scanf("%d", &num_lines);

    while ( num_lines < 7 ) {
        printf("Enter the number of lines to print\nMust be greater than or equal to 7 :  ");
        scanf("%d", &num_lines);
    }

    char symbol ;
    printf("Choose a symbol/character to be displayed */&/+/0/x :  ");
    scanf(" %c", &symbol);

    int num_symbols;
    printf("Enter the number of symbols to print per line :  ");
    scanf("%d", &num_symbols);

    if (num_symbols < 7 || num_symbols > 27) {
        num_symbols = 19;
    }
    i = 1;
    while (i <= num_lines) {
        if(i%2 == 0){
            j = i/2;
            while(j != 0){
                printf("\t");
                j--;
            }
        }
        j = 0;
        while(j < num_symbols){
            printf("%c",symbol);
            j++;
       }
        printf("\n");
        i++;
    }
    return 0;
}

Here is the output:

答案 1 :(得分:0)

在循环中,您声明变量和设置条件的方式存在问题 试着一步一步思考:
我是在一对还是在损害线上? 如果是,我把\ t \\ 3.打印x次我的号码
4.做同样的事情直到你做了足够的行

if (num_symbols < 7 || num_symbols > 27) {
    }
    int which_line = 1;
    while (num_lines > 0) {
        if (which_line % 2 == 0) { //STEP 1
         for (int tmp_line = which_line - 1; tmp_line >= 1 ; tmp_line--) { //STEP2
         printf("\t");
            }
        }
        for (int tmp_symbols = num_symbols; tmp_symbols >= 1 ; tmp_symbols--) { //STEP 3
           printf("%c", symbol); 
        }
        printf("\n");
        which_line++; //STEP 4
        --num_lines;
    }

答案 2 :(得分:-1)

^ _ ^

#include <stdio.h>

int main(void) {
    int num_lines;

    do{
        printf("Enter the number of lines to print\nMust be greater than or equal to 7 :  ");
        scanf("%d", &num_lines);
    }while(num_lines < 7);


    char symbol;
    printf("Choose a symbol/character to be displayed */&/+/0/x :  ");
    scanf(" %c", &symbol);

    int num_symbols;
    printf("Enter the number of symbols to print per line :  ");
    scanf("%d", &num_symbols);

    if (num_symbols < 7 || num_symbols > 27) {
        num_symbols = 19;
    }

    for (int i = 1; i <= num_lines; ++i) {

        if (i % 2 == 0) {
            for (int m = 0; m < i / 2; ++m) {
                printf("\t");
            }
        }

        for (int j = 0; j < num_symbols; ++j) {
            printf("%c", symbol);
        }
        printf("\n");
    }

    return 0;
}