c++ - 如何添加此代码以使用我的 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(){
}
解决方案
用行替换 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;
}
这里的输出
推荐阅读
- laravel - 无法在 Laravel 中使用 vue.js 上传多个图像
- python - 保存输入并在重新启动程序时要求再次使用它
- php - 将 Symfony services.yaml 和 security.yaml 转换为 php 格式
- reactjs - API 搜索保存不同位置的信息。(反应)
- javascript - 未捕获的类型错误:按下导航到其他页面的菜单项时无法读取未定义的属性“顶部”
- python - 我怎样才能使这段代码只适合一个有 160 个子文件夹的文件夹?
- flutter - 如何删除容器中多余的红色空间?
- python - 以给定值切割数据
- python - 如何在 ipywidgets 上制作一个自我意识按钮
- reactjs - 单击按钮时没有弹出模式