返回菜单语言 GO

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

Return to menu language GO

问题

我有一个菜单选项,有两个选项:添加和减法。当我选择其中一个选项时,程序运行正常,但会关闭。我想知道如何在操作结束后使其返回菜单,以选择另一个选项。

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. func main() {
  6. var n1, n2, s, r float64
  7. var op, ns int
  8. fmt.Println("\n\t欢迎")
  9. fmt.Println("选择一个选项")
  10. fmt.Println("1.-添加")
  11. fmt.Println("2.-减法")
  12. fmt.Scan(&op)
  13. if op == 1 {
  14. fmt.Printf("\n\t添加")
  15. fmt.Printf("\n你要添加多少个数字?")
  16. fmt.Scan(&ns)
  17. if ns <= 1 {
  18. fmt.Print("你不能只添加一个数字")
  19. } else {
  20. for i := 0; i < ns; i++ {
  21. fmt.Printf("\n输入第 %d 个数字:", i+1)
  22. fmt.Scan(&n1)
  23. s += n1
  24. }
  25. fmt.Println("\n总和是:", s)
  26. //如何返回菜单?
  27. }
  28. } else if op == 2 {
  29. fmt.Printf("\n\t减法")
  30. fmt.Printf("\n输入第一个数字:")
  31. fmt.Scan(&n1)
  32. fmt.Printf("\n输入第二个数字:")
  33. fmt.Scan(&n2)
  34. r = n1 - n2
  35. fmt.Println("\n减法结果是:", r)
  36. }
  37. }

以上是你提供的代码的翻译。

英文:

I have a menu option with two options: add and substract. When I choose one it runs ok but the program closes. I would like to know how to make it go back to the menu after an operation ends to select another one

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. )
  5. func main() {
  6. var n1, n2, s, r float64
  7. var op, ns int
  8. fmt.Println(&quot;\n\tWelcome&quot;)
  9. fmt.Println(&quot;Chose an option&quot;)
  10. fmt.Println(&quot;1.-Add&quot;)
  11. fmt.Println(&quot;2.-Substract&quot;)
  12. fmt.Scan(&amp;op)
  13. if op == 1 {
  14. fmt.Printf(&quot;\n\tAdd&quot;)
  15. fmt.Printf(&quot;\nHow many numbers you add? &quot;)
  16. fmt.Scan(&amp;ns)
  17. if ns &lt;= 1 {
  18. fmt.Print(&quot;You can not add just a number&quot;)
  19. } else {
  20. for i := 0; i &lt; ns; i++ {
  21. fmt.Printf(&quot;\nType the number %d: &quot;, i+1)
  22. fmt.Scan(&amp;n1)
  23. s += n1
  24. }
  25. fmt.Println(&quot;\nThe sum is: &quot;, s)
  26. //How to return to the menu?
  27. }
  28. } else if op == 2 {
  29. fmt.Printf(&quot;\n\tSubtraction&quot;)
  30. fmt.Printf(&quot;\nType the first number: &quot;)
  31. fmt.Scan(&amp;n1)
  32. fmt.Printf(&quot;\nType the second number: &quot;)
  33. fmt.Scan(&amp;n2)
  34. r = n1 - n2
  35. fmt.Println(&quot;\nSubstraction is: &quot;, r)
  36. }
  37. }

答案1

得分: 5

只需将整个代码块包裹在 for 循环中:

  1. for {
  2. // 代码块内容
  3. }

使用 break 来退出循环,使用 continue 来返回到循环的顶部。

英文:

Just wrap the whole thing in

  1. for {
  2. }

Use break to exit the loop or continue to go back to the top.

答案2

得分: 0

很难在没有看到你的代码的情况下给出确切的答案,但我可以假设你应该使用for ;; {}操作符,并在其中使用适当的if/else语句来处理你的菜单。

英文:

It's hard to tell exactly without seeing your code, but I may assume you should use operator for ;; {} and put inside your menu with the appropriate if/else statements.

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

发表评论

匿名网友

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

确定