在同一行打印多行字符串

标题说的是什么。我有一个骰子程序,它打印一个骰子 IE


 ------

|      |

|      |

|     1|

 ------

我希望能够打印多个骰子,所以它看起来像这样:


 ------     ------

|      |   |      |

|      |   |      |

|     1|   |     3|

 ------     ------

我试过fmt.Print,但仍然将它们打印在彼此下方。我也在考虑创建一个函数,打印每个对象的顶行,但我不知道如何做到这一点。有任何想法吗?


婷婷同学_
浏览 200回答 2
2回答

桃花长相依

这负责打印。数字在 1 到 6(含)之间随机分配。作为命令行参数的骰子数。所以在我的例子中 ./roll 6 打印 6 个骰子,随机数最多为 6。package mainimport (&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "math/rand"&nbsp; &nbsp; "os"&nbsp; &nbsp; "strconv"&nbsp; &nbsp; "time")func main() {&nbsp; &nbsp; numDice := 1&nbsp; &nbsp; if len(os.Args) > 1 {&nbsp; &nbsp; &nbsp; &nbsp; i, err := strconv.Atoi(os.Args[1])&nbsp; &nbsp; &nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(err)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; numDice = i&nbsp; &nbsp; }&nbsp; &nbsp; seed := rand.NewSource(time.Now().UnixNano())&nbsp; &nbsp; randomNumber := rand.New(seed)&nbsp; &nbsp; die := []string{&nbsp; &nbsp; &nbsp; &nbsp; " ------&nbsp; &nbsp;",&nbsp; &nbsp; &nbsp; &nbsp; "|&nbsp; &nbsp; &nbsp; |&nbsp; ",&nbsp; &nbsp; &nbsp; &nbsp; "|&nbsp; &nbsp; &nbsp; |&nbsp; ",&nbsp; &nbsp; &nbsp; &nbsp; "|&nbsp; &nbsp; &nbsp; |&nbsp; ",&nbsp; &nbsp; &nbsp; &nbsp; " ------&nbsp; &nbsp;",&nbsp; &nbsp; }&nbsp; &nbsp; for i := 0; i < 5; i++ {&nbsp; &nbsp; &nbsp; &nbsp; for j, n := 0, numDice; j < n; j++ {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if i == 3 {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Printf("|&nbsp; &nbsp; %d |&nbsp; ", randomNumber.Intn(5)+1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Print(die[i])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println()&nbsp; &nbsp; }}

慕的地10843

package mainimport (&nbsp; &nbsp; "bytes"&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "strings")func getDie(n int) []string {&nbsp; &nbsp; return []string{&nbsp; &nbsp; &nbsp; &nbsp; " ------",&nbsp; &nbsp; &nbsp; &nbsp; "|&nbsp; &nbsp; &nbsp; |",&nbsp; &nbsp; &nbsp; &nbsp; "|&nbsp; &nbsp; &nbsp; |",&nbsp; &nbsp; &nbsp; &nbsp; fmt.Sprintf("|%6d|", n),&nbsp; &nbsp; &nbsp; &nbsp; " ------",&nbsp; &nbsp; }}func joinLines(between int, items ...[]string) []string {&nbsp; &nbsp; if len(items) == 0 {&nbsp; &nbsp; &nbsp; &nbsp; return nil&nbsp; &nbsp; }&nbsp; &nbsp; if len(items) == 1 {&nbsp; &nbsp; &nbsp; &nbsp; return items[0]&nbsp; &nbsp; }&nbsp; &nbsp; lineCount := 0&nbsp; &nbsp; maxSizes := make([]int, len(items))&nbsp; &nbsp; for i, item := range items {&nbsp; &nbsp; &nbsp; &nbsp; for j, line := range item {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if maxSizes[i] < len(line) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; maxSizes[i] = len(line)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if j+1 > lineCount {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lineCount = j + 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; lines := make([]string, lineCount)&nbsp; &nbsp; for i := 0; i < lineCount; i++ {&nbsp; &nbsp; &nbsp; &nbsp; var buff bytes.Buffer&nbsp; &nbsp; &nbsp; &nbsp; for j, item := range items {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; diff := 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if j+1 < len(items) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; diff += maxSizes[j] + between&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if i < len(item) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; line := item[i]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; buff.WriteString(line)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; diff -= len(line)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if diff > 0 {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; buff.WriteString(strings.Repeat(" ", diff))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; lines[i] = buff.String()&nbsp; &nbsp; }&nbsp; &nbsp; return lines}func main() {&nbsp; &nbsp; a, b, c, d := getDie(2), getDie(3), []string{"", "", "="}, getDie(5)&nbsp; &nbsp; all := joinLines(3, a, b, c, d)&nbsp; &nbsp; for _, line := range all {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(line)&nbsp; &nbsp; }}https://play.golang.org/p/NNrTUDdfyn
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go