底线从打印字符串上剪下

我有一个打印多行字符串的程序,它切断了底部。输出:


[   ]

[██████╗  ██╗  ██╗ ██████╗  ██████╗ ]

[╚════██╗ ██║  ██║ ╚════██╗ ╚════██╗]

[ █████╔╝ ███████║  █████╔╝  █████╔╝]

[██╔═══╝  ╚════██║ ██╔═══╝   ╚═══██╗]

用于生成它的程序:


package main


import (

        "fmt"

        "strconv"

        "strings"

)


const one = `

 ██╗

███║

╚██║

 ██║

 ██║

 ╚═╝

`

const two = `

██████╗ 

╚════██╗

 █████╔╝

██╔═══╝ 

███████╗

╚══════╝    

`

const three = `

██████╗ 

╚════██╗

 █████╔╝

 ╚═══██╗

██████╔╝

╚═════╝ 

`

const four = `

██╗  ██╗

██║  ██║

███████║

╚════██║

     ██║

     ╚═╝

`

const five = `

███████╗

██╔════╝

███████╗

╚════██║

███████║

╚══════╝

`

const six = `

 ██████╗ 

██╔════╝ 

███████╗ 

██╔═══██╗

╚██████╔╝

 ╚═════╝ 

`

const seven = `

███████╗

╚════██║

    ██╔╝

   ██╔╝ 

   ██║  

   ╚═╝ 

`

const eight = `

 █████╗ 

██╔══██╗

╚█████╔╝

██╔══██╗

╚█████╔╝

 ╚════╝ 

`

const nine = `

 █████╗ 

██╔══██╗

╚██████║

 ╚═══██║

 █████╔╝

 ╚════╝ 

`

const zero = `

 ██████╗ 

██╔═████╗

██║██╔██║

████╔╝██║

╚██████╔╝

 ╚═════╝ 

`


var numbers = []string{zero, one, two, three, four, five, six, seven, eight, nine}


func getAscii(number int) [][]string {

        listOfNumbers := strings.Split(strconv.Itoa(number), "")

        var ascii [][]string

        for _, num := range listOfNumbers {

                intnum, _ := strconv.Atoi(num)

                ascii = append(ascii, strings.Split(numbers[intnum], "\n"))

        }

        return ascii

}

func LongestSlice(slices [][]string) int {

        var longest int = 0

        for _, slice := range slices {

                if len(slice) > longest {

                        longest = len(slice)

                }

        }

        return longest

}

为什么是这样?我很困惑在哪里看。我怎样才能解决这个问题,这样它就不会切断底部?抱歉,我包含了整个程序,我不知道问题出在哪里。


翻过高山走不出你
浏览 207回答 2
2回答

慕尼黑5688855

只是在循环条件下过度工程化。尝试for&nbsp;lineNumber&nbsp;:=&nbsp;0;&nbsp;lineNumber&nbsp;<=&nbsp;LongestSlice(stuff)-1;&nbsp;lineNumber++&nbsp;{在 func joinStrings 的外循环中。看到它有效https://play.golang.org/p/eR4JJtY4T1

慕村225694

对于您要完成的工作,您的解决方案似乎过于复杂。一种简单的方法是接受 evanmcdonnal 的建议并使用映射来定义整数如何转换为相应的字符串常量,如下所示:var integerToStr = map[int]string{&nbsp; 0: zero,&nbsp; 1: one,&nbsp; 2: two,&nbsp; 3: three,&nbsp; 4: four,&nbsp; 5: five,&nbsp; 6: six,&nbsp; 7: seven,&nbsp; 8: eight,&nbsp; 9: nine,}...在这种情况下,您可以像这样转换整数:if str, present := integerToStr[i]; present {&nbsp; // do something with the string} else {&nbsp; // default to something else?}如果你不喜欢包范围内的变量,你也可以使用 switch 语句在函数内部做同样的事情:var glyph stringswitch num {&nbsp; &nbsp; case "1":&nbsp; &nbsp; &nbsp; &nbsp; glyph = one&nbsp; &nbsp; case "2":&nbsp; &nbsp; &nbsp; &nbsp; glyph = two&nbsp; &nbsp; case "3":&nbsp; &nbsp; &nbsp; &nbsp; glyph = three&nbsp; &nbsp; case "4":&nbsp; &nbsp; &nbsp; &nbsp; glyph = four&nbsp; &nbsp; case "5":&nbsp; &nbsp; &nbsp; &nbsp; glyph = five&nbsp; &nbsp; case "6":&nbsp; &nbsp; &nbsp; &nbsp; glyph = six&nbsp; &nbsp; case "7":&nbsp; &nbsp; &nbsp; &nbsp; glyph = seven&nbsp; &nbsp; case "8":&nbsp; &nbsp; &nbsp; &nbsp; glyph = eight&nbsp; &nbsp; case "9":&nbsp; &nbsp; &nbsp; &nbsp; glyph = nine&nbsp; &nbsp; case "0":&nbsp; &nbsp; &nbsp; &nbsp; fallthrough&nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; glyph = zero&nbsp; &nbsp; }我还建议为您的字母制定一个标准高度,并将其与字符串本身保持一致。这使您可以通过创建一个固定长度的数组,将每个字符串的相应行附加到该数组的每个成员,然后将其转换为切片并使用“strings.Join”来完成加入最后的字符串:var out [glyphHeight]stringfor _, glyph := range glyphs {&nbsp; &nbsp; for i, line := range strings.Split(glyph, "\n") {&nbsp; &nbsp; &nbsp; &nbsp; out[i] += " " + line&nbsp; &nbsp; }}return strings.Join(out[:numGlyphLines], "\n")我认为值得一提的另一件有点迂腐的事情是,您错误地将字符串称为 ASCII。Go 实际上使用 UTF 作为其字符串。在这个例子中它并不重要,但你应该记住这一点。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go