2 回答
TA贡献1877条经验 获得超1个赞
您的人口函数有一个错误:
func deckGenerator() (deck string) {
for i := 0; i < 10; i++ {
// deck += strconv.Itoa('\U0001F0B1' + i) // converts integers to their string equivalent e.g. "127156"
deck += string(rune('\U0001F0B1' + i)) // fix
}
return deck
}
https://go.dev/play/p/E3sVRZK4exh
TA贡献1854条经验 获得超8个赞
需要的人的正确代码:
package main
import (
"fmt"
"math/rand"
"strings"
"time"
)
func main() {
var n int
var deck string
rand.Seed(int64(time.Now().Nanosecond()))
n = readNumber()
deck = deckGenerator()
drawCards(deck, n)
}
func readNumber() (n int) {
for n <= 0 || n >= 10 {
fmt.Print("Enter number between 1 and 9: ")
fmt.Scan(&n)
}
return n
}
func deckGenerator() (deck string) {
for i := 0; i < 10; i++ {
//deck += strconv.Itoa('\U0001F0B1' + i)
deck += string(rune('\U0001F0B1' + i))
}
return deck
}
func drawCards(deck string, n int) {
var cardPulledOut rune
for i := 0; i < n; i++ {
cardPulledOut, deck = drawCard(deck)
fmt.Println("Pulled out the card", string(rune(cardPulledOut)), "- Cards left in the deck:", deck)
}
}
func drawCard(deck string) (card rune, deck2 string) {
for true {
card = rune(('\U0001F0B1') + rand.Intn(10)) //random number between 0 and 9 inclusive
for _, v := range deck {
//fmt.Println(v, card)
if v == card {
deck2 = strings.Replace(deck, string(card), "", 1)
return card, deck2
}
}
}
return
}
- 2 回答
- 0 关注
- 106 浏览
添加回答
举报