package com.shuwei.dai.utils.bitmap;
import java.util.Collections;
public class StepProgressBarExample {
public static void main(String[] args) {
simulateStep("Step 1: Initializing", 10);
simulateStep("Step 2: Downloading", 20);
simulateStep("Step 3: Installing", 30);
simulateStep("Step 4: Configuring", 40);
simulateStep("Step 5: Completing", 50);
}
private static void simulateStep(String stepName, int totalTasks) {
System.out.println(stepName);
for (int i = 0; i < totalTasks; i++) {
// 模拟任务执行
simulateTaskExecution();
// 更新进度条
updateProgressBar(i + 1, totalTasks);
}
System.out.println("\n" + stepName + " completed!\n");
}
private static void simulateTaskExecution() {
// 模拟任务执行的延迟,你可以替换为实际任务的代码
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private static void updateProgressBar(int currentTask, int totalTasks) {
int progress = (int) ((double) currentTask / totalTasks * 100);
String progressBar = "[" + String.join("", Collections.nCopies(progress / 2, "=")) + ">"
+ String.join("", Collections.nCopies(50 - progress / 2, " ")) + "] " + progress + "%";
System.out.print("\r" + progressBar);
}
}
标签:simulateStep,String,进度条,int,void,打印,totalTasks,Step,Java
From: https://www.cnblogs.com/alvisClub/p/17891451.html