1

首先,我是一个初学者,试图弄清楚如何使用星号上下移动打印字母“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语句来解决这个问题,那对我会很有帮助。我还没有介绍函数、字符串和数组,我不想继续前进并变得更加困惑。谢谢大家。

4

1 回答 1

0

正如我在对您之前的帖子的评论中所说,您可能想尝试不使用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.
}

提醒:简单的设计产生简单的代码。简单的代码更容易调试并且注入的缺陷更少。

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

于 2019-09-05T16:36:56.657 回答