太难了, for input.Scan() ,输入什么, 才会执行 下面的for line,n := range counts

lilicool · · 1333 次点击
func main() { counts := make(map[string]int) input := bufio.NewScanner(os.Stdin) for input.Scan() { if input.Text()=="e"{break}//设置退出for循环 counts[input.Text()]++//输入相同的value会导致对应的key值增加 fmt.Println(counts) } // NOTE: ignoring potential errors from input.Err() for line, n := range counts { if n > 1 { fmt.Printf("%d\t%s\n", n, line) } } } //手动设置退出 ```
#1
更多评论
func main() { counts := make(map[string]int) input := bufio.NewScanner(os.Stdin) for input.Scan() { if input.Text()=="e"{break} counts[input.Text()]++//输入相同的value会导致对应的key值增加 fmt.Println(counts) } // NOTE: ignoring potential errors from input.Err() for line, n := range counts { if n > 1 { fmt.Printf("%d\t%s\n", n, line) } } }
#2