GO 编程语言中的标准 unicode

我有一个字符串,其中包含通过 unicode 从红桃 A 到红桃 10 的卡片(该练习需要使用字符串,因此没有数组或切片)给定一个数字 n,我必须从该字符串中提取 n 张卡片。如果使用 for-range 我得到的位数比我需要的少,我该怎么做?


package main


import (

    "fmt"

    "math/rand"

    "strconv"

    "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)

    }

    return deck

}


func drawCards(deck string, n int) {

    for i := 0; i < n; i++ {

        cardPulledOut, deck2 := drawCard(deck)

        fmt.Println("Pulled out the card", cardPulledOut, "- Cards left in the deck:", deck2)

    }

}


func drawCard(deck string) (cardPulledOut 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)

            /*

                output: (infinity loop)

                ...

                49 127156

                53 127156

                56 127156

                ...

            */

            if v == card {

                deck = strings.Replace(deck, string(card), "", 1)

                return cardPulledOut, deck2

            }

        }

    }

    return

}


牛魔王的故事
浏览 113回答 2
2回答

冉冉说

您的人口函数有一个错误:func deckGenerator() (deck string) {&nbsp; &nbsp; for i := 0; i < 10; i++ {&nbsp; &nbsp; &nbsp; &nbsp; // deck += strconv.Itoa('\U0001F0B1' + i) // converts integers to their string equivalent e.g. "127156"&nbsp; &nbsp; &nbsp; &nbsp; deck += string(rune('\U0001F0B1' + i))&nbsp; &nbsp; // fix&nbsp; &nbsp; }&nbsp; &nbsp; return deck}https://go.dev/play/p/E3sVRZK4exh

哔哔one

需要的人的正确代码:package mainimport (&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "math/rand"&nbsp; &nbsp; "strings"&nbsp; &nbsp; "time")func main() {&nbsp; &nbsp; var n int&nbsp; &nbsp; var deck string&nbsp; &nbsp; rand.Seed(int64(time.Now().Nanosecond()))&nbsp; &nbsp; n = readNumber()&nbsp; &nbsp; deck = deckGenerator()&nbsp; &nbsp; drawCards(deck, n)}func readNumber() (n int) {&nbsp; &nbsp; for n <= 0 || n >= 10 {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Print("Enter number between 1 and 9: ")&nbsp; &nbsp; &nbsp; &nbsp; fmt.Scan(&n)&nbsp; &nbsp; }&nbsp; &nbsp; return n}func deckGenerator() (deck string) {&nbsp; &nbsp; for i := 0; i < 10; i++ {&nbsp; &nbsp; &nbsp; &nbsp; //deck += strconv.Itoa('\U0001F0B1' + i)&nbsp; &nbsp; &nbsp; &nbsp; deck += string(rune('\U0001F0B1' + i))&nbsp; &nbsp; }&nbsp; &nbsp; return deck}func drawCards(deck string, n int) {&nbsp; &nbsp; var cardPulledOut rune&nbsp; &nbsp; for i := 0; i < n; i++ {&nbsp; &nbsp; &nbsp; &nbsp; cardPulledOut, deck = drawCard(deck)&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("Pulled out the card", string(rune(cardPulledOut)), "- Cards left in the deck:", deck)&nbsp; &nbsp; }}func drawCard(deck string) (card rune, deck2 string) {&nbsp; &nbsp; for true {&nbsp; &nbsp; &nbsp; &nbsp; card = rune(('\U0001F0B1') + rand.Intn(10)) //random number between 0 and 9 inclusive&nbsp; &nbsp; &nbsp; &nbsp; for _, v := range deck {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //fmt.Println(v, card)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if v == card {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; deck2 = strings.Replace(deck, string(card), "", 1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return card, deck2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go