如何简化这个菜单

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

How do i simplify this menu

问题

所以我正在开始一个我想做很久的项目,首先我要做的是打印一个菜单。我想知道是否有更好的编码方式来实现这段代码。为了"美化"它,我会这样调用它:

我在想要做这样的事情:

  1. fmt.Println("You have chosen option " + input)

但是我必须为每个选择的选项调用一个不同命名的函数,所以我不确定如何使其工作。

  1. func main() {
  2. fmt.Println("Hello welcome")
  3. var input int
  4. fmt.Println("Please choose an option:")
  5. fmt.Scanln(&input)
  6. if input == 1 {
  7. fmt.Println("Option 1 chosen")
  8. } else if input == 2 {
  9. fmt.Println("Option 2 chosen")
  10. } else if input == 3 {
  11. fmt.Println("Option 3 chosen")
  12. } else if input == 4 {
  13. fmt.Println("Option 4 chosen")
  14. } else if input == 5 {
  15. fmt.Println("Option 5 chosen")
  16. } else if input == 6 {
  17. fmt.Println("Option 6 chosen")
  18. } else if input == 7 {
  19. fmt.Println("Option 7 chosen")
  20. } else if input == 8 {
  21. fmt.Println("Option 8 chosen")
  22. } else if input == 9 {
  23. fmt.Println("Option 9 chosen")
  24. } else if input == 10 {
  25. fmt.Println("Option 10 chosen")
  26. } else {
  27. fmt.Print("Not an option")
  28. }
  29. }
英文:

So I'm starting a project that I've wanted to do for a while and the first thing I have to do is print a menu
I was wondering whether there was a nicer / better way of coding this code.
To "prettify" it I'd call it
I was thinking of doing something like

  1. fmt.Println("You have chosen option " + input)

But I have to call a different named function for each option chosen, so I'm not sure how I'd make this work

  1. func main() {
  2. fmt.Println("Hello welcome")
  3. var input int
  4. fmt.Println("Please choose an option:")
  5. fmt.Scanln(&input)
  6. if input == 1 {
  7. fmt.Println("Option 1 chosen")
  8. } else if input == 2 {
  9. fmt.Println("Option 2 chosen")
  10. } else if input == 3 {
  11. fmt.Println("Option 3 chosen")
  12. } else if input == 4 {
  13. fmt.Println("Option 4 chosen")
  14. } else if input == 5 {
  15. fmt.Println("Option 5 chosen")
  16. } else if input == 6 {
  17. fmt.Println("Option 6 chosen")
  18. } else if input == 7 {
  19. fmt.Println("Option 7 chosen")
  20. } else if input == 8 {
  21. fmt.Println("Option 8 chosen")
  22. } else if input == 9 {
  23. fmt.Println("Option 9 chosen")
  24. } else if input == 10 {
  25. fmt.Println("Option 10 chosen")
  26. } else {
  27. fmt.Print("Not an option")
  28. }
  29. }

答案1

得分: 3

我建议创建一个选项号到函数的映射表,像这样:

  1. func func1() {
  2. fmt.Print("选择了选项1")
  3. }
  4. func func2() {
  5. fmt.Print("选择了选项2")
  6. }
  7. func main() {
  8. funcs := map[int]func() {
  9. 1: func1,
  10. 2: func2,
  11. }
  12. fmt.Println("欢迎!")
  13. var input int
  14. fmt.Println("请选择一个选项:")
  15. fmt.Scanln(&input)
  16. f, ok := funcs[input]
  17. if !ok {
  18. fmt.Print("不是一个有效选项")
  19. } else {
  20. f()
  21. }
  22. }
英文:

I would suggest making a map of options number to functions, like this:

  1. func func1() {
  2. fmt.Print("Option 1 Chosen")
  3. }
  4. func func2() {
  5. fmt.Print("Option 2 Chosen")
  6. }
  7. func main() {
  8. funcs := map[int]func() {
  9. 1: func1,
  10. 2: func2,
  11. }
  12. fmt.Println("Hello welcome")
  13. var input int
  14. fmt.Println("Please choose an option:")
  15. fmt.Scanln(&input)
  16. f, ok := funcs[input]
  17. if !ok {
  18. fmt.Print("Not an option")
  19. } else {
  20. f()
  21. }
  22. }

huangapple
  • 本文由 发表于 2021年9月28日 21:28:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/69362671.html
匿名

发表评论

匿名网友

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

确定