0

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

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)
    }
}
4

2 回答 2

5

把整个东西包起来

for {

}

用于break退出循环或continue返回顶部。

于 2013-10-19T05:24:07.877 回答
0

如果没有看到您的代码,很难准确判断,但我可能假设您应该使用运算符并将适当的语句for ;; {}放入您的菜单中。if/else

于 2013-10-19T04:34:58.620 回答