如何检查切片中的元素并操作另一个切片(如果值存在或不存在)

我正在用 Go 编写一个程序,它应该检查切片的名称。如果该名称存在,则应在另一个切片中输入“YES”,如果不存在则应输入“NO”。名称切片应由用户输入,并且应在输入值时开始检查。


我写了一些代码,但似乎不起作用。


package main


import (

    "fmt"

)


func main() {

    var limit int

    var names string

    fmt.Scanln(&limit)

    arr := make([]string, limit)

    yn := make([]string, limit)

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

        fmt.Scanln(&names)

        for _, a := range arr {

            if a == names {

                yn = append(yn, "YES")

            } else {

                arr = append(arr, names)

                yn = append(yn, "NO")

            }

        }

    }

    fmt.Println(yn)

}

输出应该是这样的


用户输入的值:


5


史蒂夫


约翰


雷恩


珍娜


约翰


输出:






是的


我得到的错误是一个由一堆 YES 和 NO 值组成的大数组,开头有 2 个空元素。


海绵宝宝撒
浏览 101回答 1
1回答

慕盖茨4494581

第一个解决方案这就是你想要的:func main() {&nbsp; &nbsp; var limit int&nbsp; &nbsp; var name string&nbsp; &nbsp; _, err := fmt.Scanln(&limit)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(err)&nbsp; &nbsp; }&nbsp; &nbsp; var names []string&nbsp; &nbsp; var presence []string&nbsp; &nbsp; for i := 0; i < limit; i++ {&nbsp; &nbsp; &nbsp; &nbsp; _, err := fmt.Scanln(&name)&nbsp; &nbsp; &nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(err)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; var exist = false&nbsp; &nbsp; &nbsp; &nbsp; for _, a := range names {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if a == name {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exist = true&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if !exist {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; names = append(names, name)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; presence = append(presence, "NO")&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; presence = append(presence, "YES")&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; fmt.Println(presence)}首先Scanln返回 ,error您需要检查它。第二:追加到数组必须在内循环之外。尝试使用 debugPrintln()来调试您的算法。将代码移至单独的函数为了清楚起见,我建议将存在性检查移至单独的函数:func exists(a []string, element string) bool {&nbsp; &nbsp; for _, e := range a {&nbsp; &nbsp; &nbsp; &nbsp; if e == element {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return false}func main() {&nbsp; &nbsp; var limit int&nbsp; &nbsp; var name string&nbsp; &nbsp; _, err := fmt.Scanln(&limit)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(err)&nbsp; &nbsp; }&nbsp; &nbsp; var names []string&nbsp; &nbsp; var presence []string&nbsp; &nbsp; for i := 0; i < limit; i++ {&nbsp; &nbsp; &nbsp; &nbsp; _, err := fmt.Scanln(&name)&nbsp; &nbsp; &nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(err)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if !exists(names, name) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; names = append(names, name)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; presence = append(presence, "NO")&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; presence = append(presence, "YES")&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; fmt.Println(presence)}优化在前面的代码中,我们在每次循环中将字符串数组传递给函数。如果数组更大,那就不好了,最好将指向字符串数组的指针传递给函数(或指向字符串的指针数组)。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go