底线修剪打印字符串

时间:2016-05-02 21:37:36

标签: go printing slice

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

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

该程序用于生成它:

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
}

func joinStrings(stuff [][]string) [][]string {
        var joined [][]string
        for lineNumber := 0; lineNumber <= LongestSlice(joined); lineNumber++ {
                var Oneline []string
                for _, item := range stuff {
                        //err := item[lineNumber]
                        //if err != nil {
                        //      panic(err)
                        //}
                        Oneline = append(Oneline, item[lineNumber])

                }
                joined = append(joined, Oneline)
        }
        return joined
}
func main() {
        stuff := joinStrings(getAscii(2423))
        for _, line := range stuff {
                fmt.Println(line)
        }
}

这是为什么?我在哪里看起来很困惑。  我该如何解决这个问题,以免切断底部?对不起,我收录了整个程序,我不知道问题出在哪里。

2 个答案:

答案 0 :(得分:2)

在循环条件下过度生成。尝试

for lineNumber := 0; lineNumber <= LongestSlice(stuff)-1; lineNumber++ {

在func joinStrings的外部循环中。看它有效https://play.golang.org/p/eR4JJtY4T1

答案 1 :(得分:1)

对于您要完成的任务,您的解决方案似乎过于复杂。一个简单的方法是采用evanmcdonnal的建议并使用映射来定义整数如何转换为相应的字符串常量,如下所示:

if str, present := integerToStr[i]; present {
  // do something with the string
} else {
  // default to something else?
}

...在这种情况下,您可以转换这样的整数:

var glyph string
switch num {
    case "1":
        glyph = one
    case "2":
        glyph = two
    case "3":
        glyph = three
    case "4":
        glyph = four
    case "5":
        glyph = five
    case "6":
        glyph = six
    case "7":
        glyph = seven
    case "8":
        glyph = eight
    case "9":
        glyph = nine
    case "0":
        fallthrough
    default:
        glyph = zero
    }

如果您不喜欢包范围中的变量,您还可以使用switch语句在函数内部执行相同的操作:

var out [glyphHeight]string

for _, glyph := range glyphs {
    for i, line := range strings.Split(glyph, "\n") {
        out[i] += " " + line
    }
}

return strings.Join(out[:numGlyphLines], "\n")

我还建议为你的字母提供标准高度,并使字符串本身保持不变。这允许您通过创建一个固定长度的数组来水平地连接它们,使用每个字符串的相应行附加到该数组的每个成员,然后将其转换为切片并使用“strings.Join”来完成工作加入最后的字符串:

{{1}}

我认为值得一提的另一个有点迂腐的事情就是你错误地将你的字符串称为ASCII。 Go实际上使用UTF作为其字符串。在这个例子中并不重要,但你应该牢记这一点。