Golang 打印不需要的值

我是 GoLang 的新手,正在尝试编写这个程序 -


程序应该写成一个循环。在进入循环之前,程序应该创建一个大小(长度)为 3 的空整数切片。在每次通过循环期间,程序都会提示用户输入要添加到切片中的整数。程序将整数添加到切片中,对切片进行排序,并按排序顺序打印切片的内容。切片的大小必须增加以容纳用户决定输入的任意数量的整数。当用户输入字符“X”而不是整数时,程序应该只退出(退出循环)。


所以我的代码是 -


package main


import (

    "fmt"

    "sort"

    "strconv"

)


func main() {

    mySlice := make([]int, 3)

    var input string


    for i := 0; i < len(mySlice); i++ {

        fmt.Println("Please enter a number")

        fmt.Scanln(&input)

        if input == "X" {

            break

        }


        mySlice_var, err := strconv.Atoi(input)

        if err != nil {

            fmt.Println("Wrong input")

            continue

        }


        // mySlice[i] = mySlice_var

        mySlice = append(mySlice, mySlice_var)

        // mySlice[i] = mySlice_var


        sort.Ints(mySlice)

        fmt.Println(mySlice)


    }


}

例如,如果我先输入 4,然后输入 5,然后输入 1,它将打印我


Please enter a number

4

[0 0 0 4]

Please enter a number

5

[0 0 0 4 5]

Please enter a number

1

[0 0 0 1 4 5]

看起来它可以打印并排序,但是我如何摆脱前三个“0”?谢谢!


手掌心
浏览 150回答 2
2回答

江户川乱折腾

希望这是您想要打印的方式!(迭代切片,只打印非零值),试试这个!package mainimport (&nbsp; &nbsp; &nbsp; &nbsp; "fmt"&nbsp; &nbsp; &nbsp; &nbsp; "sort"&nbsp; &nbsp; &nbsp; &nbsp; "strconv")func main() {&nbsp; &nbsp; &nbsp; &nbsp; mySlice := make([]int, 3)&nbsp; &nbsp; &nbsp; &nbsp; var input string&nbsp; &nbsp; &nbsp; &nbsp; for i := 0; i < len(mySlice); i++ {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("Please enter a number")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Scanln(&input)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if input == "X" {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mySlice_var, err := strconv.Atoi(input)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("Wrong input")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // mySlice[i] = mySlice_var&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mySlice = append(mySlice, mySlice_var)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // mySlice[i] = mySlice_var&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sort.Ints(mySlice)//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(mySlice)//Iterating Over Slice , and printed only non zero values&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for _,v := range mySlice{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if v != 0 {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Printf("%v",v)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("")&nbsp; &nbsp; &nbsp; &nbsp; }}

波斯汪

mySlice := make([]int, 3)它很好地初始化一个切片,它的大小是3,然后附加3个零来初始化它像这样打印:fmt.Println(mySlice)fmt.Println("len: ", len(mySlice))fmt.Println("cap: ", cap(mySlice))[0 0 0]len:&nbsp; 3cap:&nbsp; 3你只能初始化 init capmySlice := make([]int, 0, 3)fmt.Println(mySlice)fmt.Println("len: ", len(mySlice))fmt.Println("cap: ", cap(mySlice))[]len:&nbsp; 0cap:&nbsp; 3
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go