Smile_fu 2015-12-01 07:08 采纳率: 0%
浏览 4649

正在自学go语言,命令行执行以下程序,进入等待输入状态,请问如何处理?

package main

import (
"bufio"
"fmt"
"os"
)

func main() {
counts := make(map[string]int)
input := bufio.NewScanner(os.Stdin)
for input.Scan() {
counts[input.Text()]++
}
// NOTE: ignoring potential errors from input.Err()
for line, n := range counts {
if n > 1 {
fmt.Printf("%d\t%s\n", n, line)
}
}
}

  • 写回答

1条回答 默认 最新

  • Seeker-Wu 2019-01-27 17:44
    关注

    给键盘输入加一个判断结束就可以了,然后你就正常输入,然后换行,到指定行数,就会自动运行了。
    func main() {
    var i = 0
    counts := make(map[string]int)
    input := bufio.NewScanner(os.Stdin)
    for input.Scan() {
    counts[input.Text()]++
    i += 1
    if i > 5 {
    break
    }
    }
    for line, n := range counts {
    if n > 1 {
    fmt.Printf("%d\t%s\n", n, line)
    }
    }
    }
    localhost% go run dup1.go
    this is a test
    this is a test
    this is a test
    this is not a test
    this is not a test
    this is not a test
    3 this is a test
    3 this is not a test

    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!