3
我有一个程序,正在采取一个数组和洗牌,一旦它完成了它将打印出混洗阵列的第一个值之一。一旦它打印出我想要的值,将显示“Press return to continue”消息。这条消息会一直存在,直到用户按下返回键,然后它会从混洗数组中获得下一个值。去,按返回继续
我有一个脚本为第一个值工作正常,但在按回车后,它只是在我的终端中创建空行。
这是我的例子:
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
}
端子输出:
$ go run src/RandomMeetingSpeaker/meeting.go
Paul
Press Return To Continue...
<empty line...>
<empty line...>
<empty line...>
<empty line...>
<empty line...>
etc etc
谢谢,那可以工作:) – mic