前往,按回车键继续。

huangapple go评论97阅读模式
英文:

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 (
&quot;bufio&quot;
&quot;fmt&quot;
&quot;math/rand&quot;
&quot;os&quot;
&quot;time&quot;
)
func main() {
users := make(map[int]string)
users[0] = &quot;Mike&quot;
users[1] = &quot;Paul&quot;
users[2] = &quot;Steve&quot;
users[3] = &quot;Lawrence&quot;
users[4] = &quot;Stephen&quot;
users[5] = &quot;James&quot;
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 &lt; 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(&quot;Press Return To Continue...&quot;)
reader := bufio.NewReader(os.Stdin)
input, err := reader.ReadString(&#39;.&#39;)
if err != nil {
panic(err)
}
return input
}

Terminal Output:

$ go run src/RandomMeetingSpeaker/meeting.go
Paul
Press Return To Continue...
&lt;empty line...&gt;
&lt;empty line...&gt;
&lt;empty line...&gt;
&lt;empty line...&gt;
&lt;empty line...&gt;
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(&#39;.&#39;)

with

input, err := reader.ReadString(&#39;\n&#39;)

and it will work.

huangapple
  • 本文由 发表于 2013年12月23日 20:17:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/20743400.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定