从 Java/C++ 打印到终端的进度条;
我想在屏幕上更新程序的进度百分比。
我有一个打印的进度条。当进度超过 10% 时,我添加一个 *
,但我也想写出每个数字的实际正确百分比。但是,我不想每次都写一个新数字,因为它会产生以下效果:
1
2
3
4
相反,我想在屏幕上只维护一个不断更新的数字。
我该怎么做? (…在 Java/C++ 中,但我认为这是无关紧要的)
I want to update the percentage progress of my program on the screen.
I have a progress bar that I print. I add a *
when the progress is more than 10%, but I also want to write the actual percentage correct up to every digit. However, I do not want to write a new number every time, since it will have the following effect:
1
2
3
4
Instead, I would like to just maintain one single figure on the screen that keeps getting updated.
How do I do that? (… in Java/C++, but I think it's irrelevant)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 C++ 中,我相信你想要抛出 \r 让你回到行的开头(而不是 \n),否则你会想要查看curses 库。不确定,但你也许可以在 Java 中做类似的事情。
In C++ I believe you want to throw \r to bring you back to the beginning of the line (instead of \n) or you'll want to look at the curses library. Not sure, but you can probably do something similar in Java.
输出
\b
会将光标移回一列,输出\r
会将其返回到第一列。从那里您可以打印要显示的新值。Outputting a
\b
will move the cursor back one column, and outputting a\r
will return it to the first column. From there you can print the new value to be displayed.对于C++,我认为curses提供了文本GUI或其他东西...某个地方有人已经编写了基于文本的进度条,您应该使用他们的实现,这样您就不必再次完成他们的工作。
另外,如果您不知道,java 有一个可以使用的进度条。
http://docs.oracle.com/javase/tutorial/uiswing/components /progress.html
For C++, I think the curses offers textual GUIs or something... somewhere someone has already written a textual based progress bar, you should use their implementation so you don't have to do their work over again.
Also, if you didn't know it, java has a progress bar that you can use.
http://docs.oracle.com/javase/tutorial/uiswing/components/progress.html