英文:
How to design a progress bar logic when inputs are less than 10?
问题
我正在解析一个数组中的字符串,并在解析过程中显示进度。这是我的逻辑,但对于少于10个输入的情况,它不可扩展。
在函数的初始部分已经处理了除以零的情况,即100*i/(lineLen-1)。
进度 := 0
for i := 0; i < lineLen; i++ {
//.. lineLen = 数组长度
//.....字符串处理...
if (100*i/(lineLen-1)) >= progress {
fmt.Printf("--%d%s--", progress, "%")
progress += 10
}
}
英文:
I am parsing strings from an array and showing progress as the strings are parsed. This is my logic but it doesn't scale for inputs less than 10.
Divide by zero is already taken care of during the initial part of the function for 100*i/(lineLen-1)
progress := 0
for i:= 0; i<lineLen;i++ {
//.. lineLen = array length
//.....String processing...
if (100*i/(lineLen-1)) >= progress {
fmt.Printf("--%d%s--", progress, "%")
progress += 10
}
}
答案1
得分: 1
我理解你需要将所有百分比向下取整为10的倍数。
你可以尝试以下代码:
lineLen := 4
progress := 0
for i := 0; i < lineLen; i++ {
// 向下取整到最接近的10的倍数。
actualProgress := (100 * (i+1) / lineLen)
if actualProgress >= progress {
roundedProgress := (actualProgress / 10) * 10
// 条件确保前一个进度百分比不会重复。
if roundedProgress != progress{
progress = roundedProgress
fmt.Printf("--%d%s--", progress, "%")
}
}
}
英文:
I understand that you need to floor all the percentages to a multiple of 10.
You could try something like the following.
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, "%")
}
}
}
答案2
得分: 1
请看 https://play.golang.org/p/xtRtk1T_ZW(下面是代码):
func main() {
// outputMax 是要打印的进度条项数,不包括完成度为100%的项。
// 总会至少输出两个项:0% 和 100%。
outputMax := 10
for lineLen := 1; lineLen < 200; lineLen++ {
fmt.Printf("lineLen=%-3d ", lineLen)
printProgress(lineLen, outputMax)
}
}
// 计算当前进度。
func progress(current, max int) int {
return 100 * current / max
}
// 计算组中的项数。
func groupItems(total, limit int) int {
v := total / limit
if total%limit != 0 {
v++
}
return v
}
// 打印进度条。
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%--")
}
如果你愿意,你可以使用 https://play.golang.org/p/aR6coeLhAk 对 outputMax
和 lineLen
的各种值进行循环,以查看你喜欢的 outputMax
值(我认为 8 <= outputMax < 13
看起来最好)。默认情况下,禁用了进度条的输出,但你可以在 main
函数中轻松启用它。
英文:
Have a look at https://play.golang.org/p/xtRtk1T_ZW (code reproduced below):
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%--")
}
If you want, you can execute a loop over various values of outputMax
and lineLen
using https://play.golang.org/p/aR6coeLhAk to see which value for outputMax
you like (8 <= outputMax < 13
looks best to me). Output of the progress bar is disabled by default, but you can easily enable it in main
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论