首页 > 解决方案 > 如何添加此代码以使用我的 arduino 打印完整的三角形?

问题描述

它显然只制作了半个三角形,但我不知道如何解决这个问题。我对此很陌生,老实说,我真的不知道如何工作,因此将不胜感激。我走了这么远,但现在我迷路了:

void setup() 
{
  Serial.begin(9600);
  Serial.setTimeout(100000); // timeout now is 100 seconds

  // read a number from serial port
  String s = Serial.readStringUntil(10); // read a line from serial port
  int n = s.toInt(); // convert the input string to integer value

for (int i=0; i<n; i++){
  for (int j=0; j<i+1; j++){
    Serial.print("*" );
   }

Serial.print("\n");
}

}

void loop(){

}

标签: c++arduinodrawingarduino-c++

解决方案


用行替换 n

for(int i = 1, k = 0; i <= rows; ++i, k = 0)
{
    for(space = 1; space <= rows-i; ++space)
    {
        cout <<"  ";
    }

    while(k != 2*i-1)
    {
        cout << "* ";
        ++k;
    }
    cout << endl;
}    

这里的输出

https://ideone.com/OPMeO1


推荐阅读