在Go Playground中读取用户输入

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

Read user input in The Go Playground

问题

我在Go语言中编写了我的第一个程序,但我不明白为什么当我使用Go平台(https://go.dev/play/)时,程序不等待用户输入。

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. func main() {
  6. var eleLen int
  7. var givenNUM int
  8. var TF bool
  9. fmt.Println("Enter the length of array: ")
  10. fmt.Scanln(&eleLen)
  11. arr := make([]int, eleLen)
  12. for i := 0; i < eleLen; i++ {
  13. fmt.Scanln(&arr[i])
  14. }
  15. fmt.Println("Enter number: ")
  16. fmt.Scanln(&givenNUM)
  17. for i := 0; i < eleLen; i++ {
  18. if arr[i] >= givenNUM {
  19. continue
  20. } else {
  21. TF = false
  22. }
  23. }
  24. fmt.Println(TF)
  25. }

我得到的输出是:

  1. Enter the length of array:
  2. Enter number:
  3. false
  4. Program exited.

我没有输入数组和数字的选项。

英文:

I made my first program in the Go language and I don't understand why when I use the Go platform: (https://go.dev/play/ ) the program doesn't wait for user input.

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. )
  5. func main() {
  6. var eleLen int
  7. var givenNUM int
  8. var TF bool
  9. fmt.Println(&quot;Enter the length of array: &quot;)
  10. fmt.Scanln(&amp;eleLen)
  11. arr := make([]int, eleLen)
  12. for i := 0; i &lt; eleLen; i++ {
  13. fmt.Scanln(&amp;arr[i])
  14. }
  15. fmt.Println(&quot;Enter number: &quot;)
  16. fmt.Scanln(&amp;givenNUM)
  17. for i := 0; i &lt; eleLen; i++ {
  18. if arr[i] &gt;= givenNUM {
  19. continue
  20. } else {
  21. TF = false
  22. }
  23. }
  24. fmt.Println(TF)
  25. }

The output that I get is:

  1. Enter the length of array:
  2. Enter number:
  3. false
  4. Program exited.

I don't have the option to input the array and numbers.

答案1

得分: 1

https://go.dev/play/的Playground编辑器下方的注释中写道:

> Playground程序与外部世界的唯一通信方式是通过向标准输出和标准错误输出写入信息。

英文:

The notes under the Playground editor at https://go.dev/play/ state:

> The only communication a playground program has to the outside world is by writing to standard output and standard error.

huangapple
  • 本文由 发表于 2022年3月16日 20:33:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/71497160.html
匿名

发表评论

匿名网友

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

确定