在同一行上打印多行字符串。

huangapple go评论80阅读模式
英文:

Print multi-line strings on same line

问题

标题说的是,我有一个骰子程序,它打印一个骰子,例如:

 ------
|      |
|      |
|     1|
 ------

我想要能够打印多个骰子,就像这样:

 ------     ------
|      |   |      |
|      |   |      |
|     1|   |     3|
 ------     ------

我尝试过使用fmt.Print,但它仍然会将它们打印在彼此下方。我也考虑过创建一个函数,打印每个对象的顶部线条,但我无法弄清楚如何做到这一点。有什么想法吗?

英文:

what the title says. I have a dice program, and it prints a dice IE

 ------
|      |
|      |
|     1|
 ------

I want to be able to print multiple dice, so it looks like this:

 ------     ------
|      |   |      |
|      |   |      |
|     1|   |     3|
 ------     ------

I have tried fmt.Print, but that still prints them below each other. I was thinking about creating a function as well, that prints the top line of each object, but I couldn't figure out how to do this. Any ideas?

答案1

得分: 2

这段代码负责打印输出。数字在1到6之间(包括1和6)进行随机化。骰子的数量作为命令行参数传入。所以在我的例子中,"./roll 6"会打印出6个骰子,每个骰子的随机数字在1到6之间。

package main

import (
	"fmt"
	"math/rand"
	"os"
	"strconv"
	"time"
)

func main() {
	numDice := 1

	if len(os.Args) > 1 {
		i, err := strconv.Atoi(os.Args[1])
		if err != nil {
			fmt.Println(err)
		}
		numDice = i
	}

	seed := rand.NewSource(time.Now().UnixNano())
	randomNumber := rand.New(seed)

	die := []string{
		" ------   ",
		"|      |  ",
		"|      |  ",
		"|      |  ",
		" ------   ",
	}

	for i := 0; i < 5; i++ {
		for j, n := 0, numDice; j < n; j++ {
			if i == 3 {
				fmt.Printf("|    %d |  ", randomNumber.Intn(5)+1)
			} else {
				fmt.Print(die[i])
			}
		}
		fmt.Println()
	}
}
英文:

This takes care of the printing. The numbers are randomized between 1 and 6 (inclusive). The number of dice taken as a command line argument. So in my case ./roll 6 prints 6 dice with random numbers up to 6.

package main

import (
	&quot;fmt&quot;
	&quot;math/rand&quot;
	&quot;os&quot;
	&quot;strconv&quot;
	&quot;time&quot;
)

func main() {
	numDice := 1

	if len(os.Args) &gt; 1 {
		i, err := strconv.Atoi(os.Args[1])
		if err != nil {
			fmt.Println(err)
		}
		numDice = i
	}

	seed := rand.NewSource(time.Now().UnixNano())
	randomNumber := rand.New(seed)

	die := []string{
		&quot; ------   &quot;,
		&quot;|      |  &quot;,
		&quot;|      |  &quot;,
		&quot;|      |  &quot;,
		&quot; ------   &quot;,
	}

	for i := 0; i &lt; 5; i++ {
		for j, n := 0, numDice; j &lt; n; j++ {
			if i == 3 {
				fmt.Printf(&quot;|    %d |  &quot;, randomNumber.Intn(5)+1)
			} else {
				fmt.Print(die[i])
			}
		}
		fmt.Println()
	}
}

答案2

得分: 1

package main

import (
	"bytes"
	"fmt"
	"strings"
)

func getDie(n int) []string {
	return []string{
		" ------",
		"|      |",
		"|      |",
		fmt.Sprintf("|%6d|", n),
		" ------",
	}
}

func joinLines(between int, items ...[]string) []string {
	if len(items) == 0 {
		return nil
	}
	if len(items) == 1 {
		return items[0]
	}

	lineCount := 0

	maxSizes := make([]int, len(items))
	for i, item := range items {
		for j, line := range item {
			if maxSizes[i] < len(line) {
				maxSizes[i] = len(line)
			}
			if j+1 > lineCount {
				lineCount = j + 1
			}
		}
	}

	lines := make([]string, lineCount)
	for i := 0; i < lineCount; i++ {
		var buff bytes.Buffer

		for j, item := range items {
			diff := 0
			if j+1 < len(items) {
				diff += maxSizes[j] + between
			}
			if i < len(item) {
				line := item[i]
				buff.WriteString(line)
				diff -= len(line)
			}
			if diff > 0 {
				buff.WriteString(strings.Repeat(" ", diff))
			}
		}

		lines[i] = buff.String()
	}

	return lines
}

func main() {
	a, b, c, d := getDie(2), getDie(3), []string{"", "", "="}, getDie(5)
	all := joinLines(3, a, b, c, d)
	for _, line := range all {
		fmt.Println(line)
	}
}

你可以在这里运行这段代码:https://play.golang.org/p/NNrTUDdfyn

英文:
package main
import (
&quot;bytes&quot;
&quot;fmt&quot;
&quot;strings&quot;
)
func getDie(n int) []string {
return []string{
&quot; ------&quot;,
&quot;|      |&quot;,
&quot;|      |&quot;,
fmt.Sprintf(&quot;|%6d|&quot;, n),
&quot; ------&quot;,
}
}
func joinLines(between int, items ...[]string) []string {
if len(items) == 0 {
return nil
}
if len(items) == 1 {
return items[0]
}
lineCount := 0
maxSizes := make([]int, len(items))
for i, item := range items {
for j, line := range item {
if maxSizes[i] &lt; len(line) {
maxSizes[i] = len(line)
}
if j+1 &gt; lineCount {
lineCount = j + 1
}
}
}
lines := make([]string, lineCount)
for i := 0; i &lt; lineCount; i++ {
var buff bytes.Buffer
for j, item := range items {
diff := 0
if j+1 &lt; len(items) {
diff += maxSizes[j] + between
}
if i &lt; len(item) {
line := item[i]
buff.WriteString(line)
diff -= len(line)
}
if diff &gt; 0 {
buff.WriteString(strings.Repeat(&quot; &quot;, diff))
}
}
lines[i] = buff.String()
}
return lines
}
func main() {
a, b, c, d := getDie(2), getDie(3), []string{&quot;&quot;, &quot;&quot;, &quot;=&quot;}, getDie(5)
all := joinLines(3, a, b, c, d)
for _, line := range all {
fmt.Println(line)
}
}

https://play.golang.org/p/NNrTUDdfyn

huangapple
  • 本文由 发表于 2016年3月25日 21:26:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/36220839.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定