2013-10-19 74 views
0

我有两个选项的菜单选项:添加和减去。当我选择一个它运行正常,但程序关闭。我想知道如何使它回去后,在菜单的操作结束选择另一个返回菜单语言GO

package main 

import (
    "fmt" 
) 

func main() { 
    var n1, n2, s, r float64 
    var op, ns int 

    fmt.Println("\n\tWelcome") 
    fmt.Println("Chose an option") 
    fmt.Println("1.-Add") 
    fmt.Println("2.-Substract") 
    fmt.Scan(&op) 

    if op == 1 { 

     fmt.Printf("\n\tAdd") 
     fmt.Printf("\nHow many numbers you add? ") 
     fmt.Scan(&ns) 
     if ns <= 1 { 
      fmt.Print("You can not add just a number") 

     } else { 
      for i := 0; i < ns; i++ { 
       fmt.Printf("\nType the number %d: ", i+1) 
       fmt.Scan(&n1) 
       s += n1 
      } 

      fmt.Println("\nThe sum is: ", s) 
      //How to return to the menu? 
     } 

    } else if op == 2 { 
     fmt.Printf("\n\tSubtraction") 
     fmt.Printf("\nType the first number: ") 
     fmt.Scan(&n1) 
     fmt.Printf("\nType the second number: ") 
     fmt.Scan(&n2) 
     r = n1 - n2 
     fmt.Println("\nSubstraction is: ", r) 
    } 
} 
+0

发布你的代码(包括尝试这个工作) – elithrar

回答

5

只是包装在整个事件中

for { 

} 

使用break退出循环或continue回到顶部。

+0

非常感谢:D –

0

很难确切地告诉没有看到你的代码,但我可以假设你应该使用运营商for ;; {},并把在你的菜单里面加上适当的if/else陈述。

+0

抱歉! 我拥有它 –