Algorithm 当输入小于10时,如何设计进度条逻辑?
我正在解析数组中的字符串,并在解析字符串时显示进度。这是我的逻辑,但它不适用于小于10的输入 在100*i/(lineLen-1)函数的初始部分中,已经考虑了除以零Algorithm 当输入小于10时,如何设计进度条逻辑?,algorithm,go,logic,progress-bar,Algorithm,Go,Logic,Progress Bar,我正在解析数组中的字符串,并在解析字符串时显示进度。这是我的逻辑,但它不适用于小于10的输入 在100*i/(lineLen-1)函数的初始部分中,已经考虑了除以零 进度:=0 对于i:=0;i=进展{ fmt.Printf(“--%d%s--”,进度,%”) 进度+=10 } } 我知道您需要将所有百分比限定为10的倍数 你可以试试下面的方法 lineLen := 4 progress := 0 for i := 0; i < lineLen; i++ { // Rounding
进度:=0
对于i:=0;i=进展{
fmt.Printf(“--%d%s--”,进度,%”)
进度+=10
}
}
我知道您需要将所有百分比限定为10的倍数
你可以试试下面的方法
lineLen := 4
progress := 0
for i := 0; i < lineLen; i++ {
// Rounding down to the nearest multiple of 10.
actualProgress := (100 * (i+1) / lineLen)
if actualProgress >= progress {
roundedProgress := (actualProgress / 10) * 10
// Condition to make sure the previous progress percentage is not repeated.
if roundedProgress != progress{
progress = roundedProgress
fmt.Printf("--%d%s--", progress, "%")
}
}
}
lineLen:=4
进度:=0
对于i:=0;i=进度{
四舍五入进度:=(实际进度/10)*10
//条件以确保上一个进度百分比不会重复。
如果为roundedProgress!=进度{
进度=四舍五入的进度
fmt.Printf(“--%d%s--”,进度,%”)
}
}
}
我知道您需要将所有百分比限定为10的倍数 你可以试试下面的方法
lineLen := 4
progress := 0
for i := 0; i < lineLen; i++ {
// Rounding down to the nearest multiple of 10.
actualProgress := (100 * (i+1) / lineLen)
if actualProgress >= progress {
roundedProgress := (actualProgress / 10) * 10
// Condition to make sure the previous progress percentage is not repeated.
if roundedProgress != progress{
progress = roundedProgress
fmt.Printf("--%d%s--", progress, "%")
}
}
}
lineLen:=4
进度:=0
对于i:=0;i=进度{
四舍五入进度:=(实际进度/10)*10
//条件以确保上一个进度百分比不会重复。
如果为roundedProgress!=进度{
进度=四舍五入的进度
fmt.Printf(“--%d%s--”,进度,%”)
}
}
}
看一看(代码复制如下):
func main(){
//outputMax是要打印的进度项目数,不包括100%完成项目。
//始终至少有两项输出:0%和100%。
输出最大值:=10
对于lineLen:=1;lineLen<200;lineLen++{
fmt.Printf(“lineLen=%-3d”,lineLen)
打印进度(lineLen、outputMax)
}
}
//计算当前进度。
函数进度(当前,最大整数)整数{
返回100*当前/最大值
}
//计算组中的项目数。
func groupItems(总计,限制整数)整数{
v:=总额/限额
如果总百分比限制!=0{
五++
}
返回v
}
//打印进度条。
func打印进度(lineLen、outputMax int){
itemsPerGroup:=groupItems(lineLen,outputMax)
对于i:=0;i
如果需要,您可以在outputMax
和lineLen
的不同值上执行循环,以查看您喜欢的outputMax
的哪个值(8查看一下(下面复制的代码):
func main(){
//outputMax是要打印的进度项目数,不包括100%完成项目。
//始终至少有两项输出:0%和100%。
输出最大值:=10
对于lineLen:=1;lineLen<200;lineLen++{
fmt.Printf(“lineLen=%-3d”,lineLen)
打印进度(lineLen、outputMax)
}
}
//计算当前进度。
函数进度(当前,最大整数)整数{
返回100*当前/最大值
}
//计算组中的项目数。
func groupItems(总计,限制整数)整数{
v:=总额/限额
如果总百分比限制!=0{
五++
}
返回v
}
//打印进度条。
func打印进度(lineLen、outputMax int){
itemsPerGroup:=groupItems(lineLen,outputMax)
对于i:=0;i
如果需要,可以在outputMax
和lineLen
的不同值上执行循环,以查看您喜欢的outputMax
的值(8以防万一你不想花时间构建一个健壮的进度条..它已经做得很好了,我也建议使用现有的库。没有理由重新发明轮子。以防万一你不想花时间构建一个健壮的进度条..它已经做得很好了,我建议使用还有一个现有的图书馆。没有理由重新发明轮子。
func main() {
// outputMax is the number of progress items to print, excluding the 100% completion item.
// There will always be at least 2 items output: 0% and 100%.
outputMax := 10
for lineLen := 1; lineLen < 200; lineLen++ {
fmt.Printf("lineLen=%-3d ", lineLen)
printProgress(lineLen, outputMax)
}
}
// Calculate the current progress.
func progress(current, max int) int {
return 100 * current / max
}
// Calculate the number of items in a group.
func groupItems(total, limit int) int {
v := total / limit
if total%limit != 0 {
v++
}
return v
}
// Print the progress bar.
func printProgress(lineLen, outputMax int) {
itemsPerGroup := groupItems(lineLen, outputMax)
for i := 0; i < lineLen; i++ {
if i%itemsPerGroup == 0 {
fmt.Printf("--%d%%--", progress(i, lineLen))
}
}
fmt.Println("--100%--")
}