从 Java/C++ 打印到终端的进度条;

发布于 2024-12-04 21:27:54 字数 239 浏览 1 评论 0原文

我想在屏幕上更新程序的进度百分比。

我有一个打印的进度条。当进度超过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

故事和酒 2024-12-11 21:27:54

在 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.

不交电费瞎发啥光 2024-12-11 21:27:54

输出 \b 会将光标移回一列,输出 \r 会将其返回到第一列。从那里您可以打印要显示的新值。

std::cout << "111\r22\r3" << std::endl;

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.

std::cout << "111\r22\r3" << std::endl;
一人独醉 2024-12-11 21:27:54

对于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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文