英文:
Go, 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("按回车键继续...")
reader := bufio.NewReader(os.Stdin)
input, err := reader.ReadString('\n')
if err != nil {
panic(err)
}
return input
}
终端输出:
$ go run src/RandomMeetingSpeaker/meeting.go
Paul
按回车键继续...
<空行>
<空行>
<空行>
<空行>
<空行>
等等等等
希望这可以帮助到你。
英文:
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
得分: 5
ReadString
接受分隔符字节作为参数。在你的情况下,分隔符是换行符,而不是点号。只需将以下代码行中的点号替换为换行符:
input, err := reader.ReadString('.')
改为
input, err := reader.ReadString('\n')
然后它将正常工作。
英文:
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('\n')
and it will work.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论