我无法使该程序打印带有选项卡的符号行。我附上一张应该如何打印的图片。当前它正在工作,除了没有缩进。任何帮助将不胜感激。
这个想法是显示带有缩进(\ 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;
}
最佳答案
^ _ ^
#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;
}
关于c - 在C中不执行While循环,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47047796/