c - C 中的 While 循环不执行

标签 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;
}

最佳答案

在您的循环中,声明变量和放置条件的方式存在问题。
尝试一步步思考:
1. 我是在双线还是弱线上?
2.如果是,我输入\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;
    }

关于c - C 中的 While 循环不执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47047796/

相关文章:

c - 如何修复错误代码 C4146 "unary minus operator applied to unsigned type.result still unsigned"?

c - 将结构传递给另一个 .c 文件中的函数,但它不指向正确的结构

c - 将图像的一部分写入文件

c++ - 设置GL上下文时 'framebuffer'的目的是什么?

c - 在函数调用中使用从函数返回的指针

c - 使用 C 语言进行自动化 2D 机器运动

检查 Linux 中的端口可用性

c - pthread_create 函数格式和指针 - C Linux POSIX 库

c - 1到n之间素数的个数

检查一个字符串在 C 中是否只有空白字符