drt96856 2013-10-19 04:00
浏览 8
已采纳

返回菜单语言GO

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

package main

import (
    "fmt"
)

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

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

    if op == 1 {

        fmt.Printf("
\tAdd")
        fmt.Printf("
How 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("
Type the number %d: ", i+1)
                fmt.Scan(&n1)
                s += n1
            }

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

    } else if op == 2 {
        fmt.Printf("
\tSubtraction")
        fmt.Printf("
Type the first number: ")
        fmt.Scan(&n1)
        fmt.Printf("
Type the second number: ")
        fmt.Scan(&n2)
        r = n1 - n2
        fmt.Println("
Substraction is: ", r)
    }
}
  • 写回答

2条回答 默认 最新

  • dsj83686 2013-10-19 05:24
    关注

    Just wrap the whole thing in

    for {
    
    }
    

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 制裁名单20240508芯片厂商
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致