gpt4 book ai didi

C 金字塔字符模式

转载 作者:行者123 更新时间:2023-11-30 17:18:53 25 4
gpt4 key购买 nike

所以,经过几个小时的工作,我终于可以制作图案了。但是,即使使用 _getchar() 函数后,我也无法关闭输出窗口。下面是我的代码:

#include <stdio.h>
#include <conio.h>
int main()
{
char ch, j = ' ';
char ch1 = 'A';
char row;
char col1;
char spaces;
char col2;
printf("Enter a character\n");
while (scanf_s("%c", &ch, 1) == 1)
{
for (row = 'A'; row <= ch; row++)
{
// This stmt was only tracker printf("%c", row);
for (spaces = ch; spaces >= row; spaces--)
{
printf("%c", j);
}
for (col1 = ch1; col1 <= row; col1++)
{
printf("%c", col1);
}

for (col2 = col1 - 2; col2 >= ch1; --col2)
{
printf("%c", col2);
}
printf("\n");
}
break;
}
_getch();
return 0;
}

我觉得任务完成了,这是一个更棘手的问题。我仍然不高兴使用break语句。每当在屏幕上形成图案时,只需按一下返回键就可以中断输出,但事实并非如此。我错过了什么?

最佳答案

这是我用来完成所需功能的代码

    #include <stdio.h>

int main()
{
char ch;
char j = ' ';
char ch1 = 'A';
char row;
char col1;
char spaces;
char col2;

printf("Enter a character in the range (inclusive) A...Z\n");
if( (scanf(" %c", &ch) == 1) && (ch >= 'A') && (ch <= 'Z' ) )
{
for (row = 'A'; row <= ch; row++)
{
printf("%c", row);
for (spaces = ch; spaces >= row; spaces--)
{
printf("%c", j);
}
for (col1 = ch1; col1 <= row; col1++)
{
printf("%c", col1);
}

for (col2 = col1 - 2; col2 >= ch1; --col2)
{
printf("%c", col2);
}
printf("\n");
}
break;
}

getchar();
return 0;
} // end function: main

关于C 金字塔字符模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29053402/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com