如何使程序等待进一步的输入?

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

How to make program wait for further input?

问题

如果你想让程序在等待其他问题的答案后再继续执行,你可以按照以下方式修改代码:

  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "os"
  6. "time"
  7. )
  8. func main() {
  9. scanner := bufio.NewScanner(os.Stdin)
  10. fmt.Printf("How long did the christians walk for the crusade? :")
  11. scanner.Scan()
  12. input := scanner.Text()
  13. if input == "3 years" {
  14. fmt.Println("Correct!")
  15. } else {
  16. fmt.Println("Incorrect.")
  17. return // 添加 return 语句,结束程序
  18. }
  19. fmt.Printf("How many of the crusades were won by christians? :")
  20. scanner.Scan()
  21. input = scanner.Text()
  22. if input == "1" {
  23. fmt.Println("Correct!")
  24. } else {
  25. fmt.Println("Incorrect.")
  26. return // 添加 return 语句,结束程序
  27. }
  28. fmt.Printf("Who started the crusades? :")
  29. scanner.Scan()
  30. input = scanner.Text()
  31. if input == "the pope" {
  32. fmt.Println("Correct!")
  33. } else {
  34. fmt.Println("Incorrect.")
  35. return // 添加 return 语句,结束程序
  36. }
  37. fmt.Printf("Thank you for playing!")
  38. time.Sleep(2 * time.Hour)
  39. }

通过在每个问题后添加 return 语句,如果答案不正确,程序将会提前结束。这样就可以等待用户输入每个问题的答案后再继续执行。

英文:

If I run the below program and enter an answer (e.g. "3 years") the program completes without waiting for any further input e.g.:

  1. How long did the christians walk for the crusade? :3 years
  2. Correct!
  3. How many of the crusades were won by christians? :Incorrect.
  4. Who started the crusades? :Incorrect.
  5. Thank you for playing!

How can I make it wait for answers to the other questions?

  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "os"
  6. "time"
  7. )
  8. func main() {
  9. scanner := bufio.NewScanner(os.Stdin)
  10. fmt.Printf("How long did the christians walk for the crusade? :")
  11. scanner.Scan()
  12. input := scanner.Text()
  13. if input == "3 years" {
  14. fmt.Println("Correct!")
  15. } else {
  16. fmt.Println("Incorrect.")
  17. }
  18. fmt.Printf("How many of the crusades were won by christians? :")
  19. if input == "1" {
  20. fmt.Println("Correct!")
  21. } else {
  22. fmt.Println("Incorrect.")
  23. }
  24. fmt.Printf("Who started the crusades? :")
  25. if input == "the pope" {
  26. fmt.Println("Correct!")
  27. } else {
  28. fmt.Println("Incorrect.")
  29. }
  30. fmt.Printf("Thank you for playing!")
  31. time.Sleep(2 * time.Hour)
  32. }

答案1

得分: 1

以上代码将接受一行输入,并将结果存储在变量input中。

所以当你的程序运行时,它会问第一个问题并等待答案;假设我回答“1天”,那么input == "1天"。在此之后,input的值不会改变-你正在将用户输入的第一个值与多个答案进行比较,即"1天" == "3年""1天" == "教皇"

如果你想接受额外的行,你需要重复调用scanner.Scan(),例如:

  1. scanner.Scan()
  2. input := scanner.Text()
  3. if input == "3年" {
  4. fmt.Println("正确!")
  5. } else {
  6. fmt.Println("错误。")
  7. }
  8. fmt.Printf("十字军中有多少次是基督徒赢得了胜利?:")
  9. scanner.Scan() // 从scanner获取下一个答案
  10. input = scanner.Text()
  11. if input == "1" {
  12. fmt.Println("正确!")
  13. } else {
  14. fmt.Println("错误。")
  15. }

请注意,你可以通过使用一个函数来简化这个过程:

  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "os"
  6. )
  7. func main() {
  8. s := bufio.NewScanner(os.Stdin)
  9. askQuestion(s, "基督徒为了十字军而行走了多久?:", "3年")
  10. askQuestion(s, "十字军中有多少次是基督徒赢得了胜利?:", "1")
  11. askQuestion(s, "谁发起了十字军?:", "教皇")
  12. fmt.Printf("谢谢参与!")
  13. }
  14. func askQuestion(s *bufio.Scanner, question string, answer string) {
  15. fmt.Print(question)
  16. if !s.Scan() {
  17. panic(fmt.Sprintf("扫描失败:%s", s.Err()))
  18. }
  19. if s.Text() == answer {
  20. fmt.Println("正确!")
  21. } else {
  22. fmt.Println("错误。")
  23. }
  24. }
英文:
  1. scanner.Scan()
  2. input := scanner.Text()

The above will accept a single line of input and store the result in the variable input.

So when your program runs it asks the first question and waits for an answer; lets say I answer with "1 day" then input == "1 day". The value of Input is not changed after that point - you are comparing the first value the user enters against multiple answers i.e. "1 day" == "3 years" and "1 day" == "the pope".

If you want to accept additional lines you will need to repeat the call e.g.

  1. scanner.Scan()
  2. input := scanner.Text()
  3. if input == "3 years" {
  4. fmt.Println("Correct!")
  5. } else {
  6. fmt.Println("Incorrect.")
  7. }
  8. fmt.Printf("How many of the crusades were won by christians? :")
  9. scanner.Scan() // Get next answer from scanner
  10. input = scanner.Text()
  11. if input == "1" {
  12. fmt.Println("Correct!")
  13. } else {
  14. fmt.Println("Incorrect.")
  15. }

Note that you can simplify this considerably by using a function:

  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "os"
  6. )
  7. func main() {
  8. s := bufio.NewScanner(os.Stdin)
  9. askQuestion(s, "How long did the christians walk for the crusade? :", "3 years")
  10. askQuestion(s, "How many of the crusades were won by christians? :", "1")
  11. askQuestion(s, "Who started the crusades? :", "the pope")
  12. fmt.Printf("Thank you for playing!")
  13. }
  14. func askQuestion(s *bufio.Scanner, question string, answer string) {
  15. fmt.Print(question)
  16. if !s.Scan() {
  17. panic(fmt.Sprintf("scan failed: %s", s.Err()))
  18. }
  19. if s.Text() == answer {
  20. fmt.Println("Correct!")
  21. } else {
  22. fmt.Println("Incorrect.")
  23. }
  24. }

huangapple
  • 本文由 发表于 2022年2月13日 09:20:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/71097185.html
匿名

发表评论

匿名网友

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

确定