gpt4 book ai didi

c++ - 打印星号中上下移动的字母

转载 作者:太空宇宙 更新时间:2023-11-04 12:33:13 24 4
gpt4 key购买 nike

首先,我是一个初学者,试图弄清楚如何使用星号打印上下移动的字母“OK”。目标是从偏移位置开始并让它们递增和递减 x 次。 (我还没有参与这部分)。但首先,我一直在思考为什么“K”没有按照我想要的方式打印出来。谁能提供帮助?

我尝试设置一个 for 循环来处理每个字符的打印。我试图制作计数器,以便在打印字符时它们看起来是偏移的,因为我知道如果它们都从第 0 行开始,那么它们将并排打印。以下是我到目前为止编写的一些代码。 'O' 打印但 'K' 打印奇怪。

int main() {
char userInputCharacter; // User input for some character
int userInputNumber; // User input for some numerical value

cout << endl
<< "Choose from among the following options: \n"

<< " 2. Display OK as an animation \n"

<< "Your choice -> ";
cin >> userInputCharacter;
cout << endl;


// Display OK as an animation
if( userInputCharacter == '2') {
cout << "How many sets of letters do you want to display? -> ";
cin >> userInputNumber;

for( int setsOfLetters = 0; setsOfLetters < userInputNumber; setsOfLetters++) {

// Display some number of blank lines. This starts as a large number the first time, then
// gets smaller each subsequent time, moving the ENTIRE set of letters vertically.
for( int numberOfBlankLines = userInputNumber; numberOfBlankLines > setsOfLetters; numberOfBlankLines--) {
cout << endl;
}

// Display one set of letters, going through and printing one "slice" of each letter at a time.

int i = 0;
int j = 4;
for( int i=0; i<8; i++) {
if( i==0) cout << " ";
else if( i==1) cout << " ";
else if( i==2) cout << " ** ";
else if( i==3) cout << " * * ";
else if( i==4) cout << "* * ";
else if( i==5) cout << "* * ";
else if( i==6) cout << " * * ";
else if( i==7) cout << " ** ";


if( j ==0) cout << " ";
else if( j ==1) cout<< " ";
else if( j ==2) cout << "* * ";
else if( j ==3) cout << "* * ";
else if( j ==4) cout << "** ";
else if( j ==5) cout << "* * ";
else if( j ==6) cout << "* * ";
else if( j ==7) cout << " ";

cout << endl;


}//end for( int i...)

// Clear the screen after the letters are displayed.
this_thread::sleep_for(chrono::milliseconds( 185)); // Sleep for 185 milliseconds
system( "clear"); // Clear the screen. Comment out this line if you don't want them erased.

}//end for( int setsOfLetters...

}//end else if( userInputCharacter == '2' ...

return 0;
}//end main()

如果我们可以建议使用 for 循环和 ifelse 语句来解决这个问题,那将对我有好处。我还没有涉及函数和字符串,也没有涉及数组,我不想跳到前面而变得更加困惑。提前谢谢大家。

最佳答案

正如我在对您之前帖子的评论中所述,您可能想尝试在没有 for 的情况下打印循环:

std::cout << "  **   *  * \n";
std::cout << " * * * * \n";
std::cout << "* * ** \n";
std::cout << "* * * * \n";
std::cout << " * * * * \n";
std::cout << " ** * *\n";

可能,对您来说更简单的解决方案是将文本声明为 C 样式字符串:

static const char ok_lines[] =
{
" ** * * \n"
" * * * * \n"
"* * ** \n"
"* * * * \n"
" * * * * \n"
" ** * *\n"
};

然后您可以在之前打印行:

std::cout << "\n\n";
std::cout << ok_line;

或打印以下行:

std::cout << ok_line;
std::cout << "\n\n";

您的动画可能看起来像:

static const unsigned int MAX_CYCLES = 10U;
for (unsigned int i = 0; i < MAX_CYCLES; ++i)
{
Clear_The_Screen();
if ((i % 2) == 0)
{
std::cout << ok_lines;
std::cout << "\n\n";
}
else
{
std::cout << "\n\n";
std::cout << ok_lines;
}
Delay(); // You'll need to write or research this one.
}

提醒:简单的设计产生简单的代码。简单的代码更容易调试,注入(inject)的缺陷也更少。

注意:如果你想要更高效的输出,替换std::cout << ok_lines;std::cout.write(ok_lines, sizeof(ok_lines) - 1); . -1防止将 nul 终止符写入控制台。

关于c++ - 打印星号中上下移动的字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57809077/

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