英文:
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 (
"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()
}
}
答案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 (
"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)
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论