dtkp51520 2013-12-23 12:17
浏览 41
已采纳

继续,按回车键继续

I have a program that is taking an array and shuffling it, once it has done this it will print out one of the first value from the shuffled array. Once it prints out the value I want to it display a 'Press return to continue' message will be displayed. This message will presist until the user presses return, then it will get the next value from the shuffled array.

I have a script working fine for the first value but after I press return it just creates empty lines in my terminal.

Here is my example:

package main

import (
    "bufio"
    "fmt"
    "math/rand"
    "os"
    "time"
)

func main() {

    users := make(map[int]string)

    users[0] = "Mike"
    users[1] = "Paul"
    users[2] = "Steve"
    users[3] = "Lawrence"
    users[4] = "Stephen"
    users[5] = "James"

    getNextSpeaker(users)

}

func getNextSpeaker(users map[int]string) {
    numUsers := len(users)
    list := randList(1, numUsers)

    for _, element := range list {
        fmt.Println(users[element-1])
        pressAnyKey()
    }

}

func randList(min, max int) []int {
    if max < min {
        min, max = max, min
    }
    length := max - min + 1
    t0 := time.Now()
    rand.Seed(int64(t0.Nanosecond()))
    list := rand.Perm(length)
    for index, _ := range list {
        list[index] += min
    }

    return list
}

func pressAnyKey() string {
    fmt.Println("Press Return To Continue...")
    reader := bufio.NewReader(os.Stdin)
    input, err := reader.ReadString('.')
    if err != nil {
        panic(err)
    }

    return input
}

Terminal Output:

$ go run src/RandomMeetingSpeaker/meeting.go
Paul
Press Return To Continue...
<empty line...>
<empty line...>
<empty line...>
<empty line...>
<empty line...>
etc etc
  • 写回答

1条回答 默认 最新

  • douxi8119 2013-12-23 12:25
    关注

    ReadString takes the delimiter byte. In your case, that's a newline, not a dot. Simply replace the line

    input, err := reader.ReadString('.')
    

    with

    input, err := reader.ReadString('
    ')
    

    and it will work.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测