在单独的行上循环时;需要在同一条线上
While loop looping on Separate Lines; Need to be on the Same Line
>我需要我的代码在同一行上一个接一个地输出"栅栏柱",但它们在不同的行上。这是我到目前为止的代码:
#include <iostream>
using namespace std;
int main(void) {
int posts;
cout << "nI can make a fence with from 2 to 18 posts.n";
cout << "How many posts would you like to have in your fence? ";
cin >> posts ;
if ((posts > 1) && (posts < 19)) {
while ((posts > 1) && (posts < 19)) {
cout << "|---" ;
posts = posts - 1;
cout << "|n" ;
}
}
else {
cout << "nnSorry, no can do.n";
}
}
这就是它输出的内容:
I can make a fence with from 2 to 18 posts.
How many posts would you like to have in your fence? 4
|---|
|---|
|---|
将 cout <<"|" 移到 while 循环之外。
while ((posts > 1) && (posts < 19))
{
cout << "|---" ;
(posts = posts - 1) ;
}
cout << "|n" ;
}
将
内部循环替换为:
while (--posts)
cout << "|---";
cout << "|n";
相关文章:
- 如何在 c++ 中确定一条指令(以字节为单位)在哪里结束,另一条指令从哪里开始?
- 如何在 Win32 中用一条线连接 2 个文本框?
- C++ 如何在同一行中和在一条线上?
- 如何在SDL2窗口上使用Cairo绘制一条白色直线
- 在Opengl中绘制一条不显示C++的线
- 为什么当 while 循环中的 if 条件变为 false 时,我的函数不输出最后一条语句?
- x86 - 为什么编译器在下一条指令中插入看似毫无意义的JMP?
- CMake似乎忽略了一条线
- 在C 中使用Legacy OpenGL(即时模式)绘制一条厚线
- 在图像标签上画一条线并计算距离
- switch 语句结束和下一条语句之间的时间是否具有确定性(与路径无关)
- 如何在OMNET++中收到来自前一个模块的所有消息后将一条消息发送到下一个模块
- 一条线和两行声明之间的差异
- C++ OpenCV:检测两条线而不是一条线(Canny & findContours)
- 如何在C++中将边列表转换为邻接矩阵,其中包含连接到顶点的边数而不是一条边?
- 为什么 fstream 打印出文件的最后一条记录
- 如何在另一条消息(M1)之前调用带有消息(M2)的lambda函数,但输出要"M2 M1"?
- 如何在子小部件而不是父窗口小部件上绘制一条线
- 在C++中,文件中的下一条记录被非法更新
- MFC:在 CRichEditCtrl 中突出显示一条线一段时间,然后恢复