如何在 Java 控制台应用程序中制作进度条?
How to make a progress bar in Java console application?
我正在尝试制作这样的进度条
[# ] 10%
[##### ] 50%
[#########] 100%
我试过的
public static void main(String[] args) throws InterruptedException {
String format = "[# ]%d%%\r";
for(int i=0;i<=100;i++){
System.out.print(String.format(format, i));
Thread.sleep(10);
}
}
输出:
[# ] 10%
[# ] 50%
[# ] 100%
问题是我无法根据进度增加 #
计数。
那么我如何移动或增加#?
您需要编写一段代码,生成从 #
开始到空格结束的十个字符 String
。向此方法传递一个从 0 到 100 的数字。该方法应将该数字除以十,然后向上舍入结果。这将为您提供十个字符栏中 #
个字符的数量:
int numPounds = (pct + 9) / 10;
做一个循环追加'#'
numPounds
次,然后追加' '
直到字符串的长度为十。在 [
... ]
个字符之间打印结果以完成练习。
private static final StringBuilder res = new StringBuilder();;
static String progress(int pct) {
res.delete(0, res.length());
int numPounds = (pct + 9) / 10;
for (int i = 0 ; i != numPounds ; i++) {
res.append('#');
}
while (res.length() != 10) {
res.append(' ');
}
return res.toString();
}
public static void main (String[] args) throws java.lang.Exception
{
for (int i = 0 ; i <= 100 ; i++) {
Thread.sleep(10);
System.out.print(String.format("[%s]%d%%\r", progress(i), i));
}
}
看看这个
static int current=0;
static int previous=0;
static String previousString="";
public static void main(String[] args) throws InterruptedException {
String format = "[%s]%d%%\r";
for (int i = 0; i <= 100; i++) {
try {
current=i/10;
System.out.print(String.format(format, repeat("#",current ), i));
} catch (ArithmeticException e) {
System.out.print(String.format(format, repeat("#", 0), i));
}
Thread.sleep(10);
}
}
static String repeat(String StringToRepat, int repetition) {
if (repetition==previous)
{
return previousString;
}
StringBuilder builder = new StringBuilder("");
for (int i = 0; i < repetition; i++)
builder.append(StringToRepat);
previousString=builder.toString();
previous=repetition;
return previousString;
}
所有学分归于@Poshemo
public static void main(String[] args) throws InterruptedException {
final StringBuilder sb = new StringBuilder();
String format = "[%-11s]%d%%\r";
for(int i=0;i<=100;i++){
if(i%10==0){
sb.append("#");
}
System.out.print(String.format(format, sb, i));
Thread.sleep(10);
}
}
我需要一个项目的进度条,我只是把它放在 Github 上,以防它能帮助到别人。
基本思路是把你的主任务分成子任务,你只要通知栏子任务的进度,显示就自动完成了。
随机计时的示例用法:
ConsoleProgressBar bar = new ConsoleProgressBar(numberOfSubtasks);
bar.startAndDisplay();
for (int i = 0; i < numberOfSubtasks; i += increment) {
bar.updateAndDisplay(increment);
Thread.sleep(minrandomTime + rand.nextInt(maxrandomTime - minrandomTime));
}
我正在尝试制作这样的进度条
[# ] 10%
[##### ] 50%
[#########] 100%
我试过的
public static void main(String[] args) throws InterruptedException {
String format = "[# ]%d%%\r";
for(int i=0;i<=100;i++){
System.out.print(String.format(format, i));
Thread.sleep(10);
}
}
输出:
[# ] 10%
[# ] 50%
[# ] 100%
问题是我无法根据进度增加 #
计数。
那么我如何移动或增加#?
您需要编写一段代码,生成从 #
开始到空格结束的十个字符 String
。向此方法传递一个从 0 到 100 的数字。该方法应将该数字除以十,然后向上舍入结果。这将为您提供十个字符栏中 #
个字符的数量:
int numPounds = (pct + 9) / 10;
做一个循环追加'#'
numPounds
次,然后追加' '
直到字符串的长度为十。在 [
... ]
个字符之间打印结果以完成练习。
private static final StringBuilder res = new StringBuilder();;
static String progress(int pct) {
res.delete(0, res.length());
int numPounds = (pct + 9) / 10;
for (int i = 0 ; i != numPounds ; i++) {
res.append('#');
}
while (res.length() != 10) {
res.append(' ');
}
return res.toString();
}
public static void main (String[] args) throws java.lang.Exception
{
for (int i = 0 ; i <= 100 ; i++) {
Thread.sleep(10);
System.out.print(String.format("[%s]%d%%\r", progress(i), i));
}
}
看看这个
static int current=0;
static int previous=0;
static String previousString="";
public static void main(String[] args) throws InterruptedException {
String format = "[%s]%d%%\r";
for (int i = 0; i <= 100; i++) {
try {
current=i/10;
System.out.print(String.format(format, repeat("#",current ), i));
} catch (ArithmeticException e) {
System.out.print(String.format(format, repeat("#", 0), i));
}
Thread.sleep(10);
}
}
static String repeat(String StringToRepat, int repetition) {
if (repetition==previous)
{
return previousString;
}
StringBuilder builder = new StringBuilder("");
for (int i = 0; i < repetition; i++)
builder.append(StringToRepat);
previousString=builder.toString();
previous=repetition;
return previousString;
}
所有学分归于@Poshemo
public static void main(String[] args) throws InterruptedException {
final StringBuilder sb = new StringBuilder();
String format = "[%-11s]%d%%\r";
for(int i=0;i<=100;i++){
if(i%10==0){
sb.append("#");
}
System.out.print(String.format(format, sb, i));
Thread.sleep(10);
}
}
我需要一个项目的进度条,我只是把它放在 Github 上,以防它能帮助到别人。
基本思路是把你的主任务分成子任务,你只要通知栏子任务的进度,显示就自动完成了。
随机计时的示例用法:
ConsoleProgressBar bar = new ConsoleProgressBar(numberOfSubtasks);
bar.startAndDisplay();
for (int i = 0; i < numberOfSubtasks; i += increment) {
bar.updateAndDisplay(increment);
Thread.sleep(minrandomTime + rand.nextInt(maxrandomTime - minrandomTime));
}