dongwei4444 2019-06-12 23:20
浏览 34
已采纳

该程序检查是否可以使用给定长度的边来构建三角形。 如何缩短中频条件?

I wrote a simple GO program to asks the user for three integers (firstNum, secondNum, and thirdNum). I'm using the triangle inequality to determine if a triangle can be built using those three integers:

A (firstNum) + B (secondNum) > C (thirdNum)

A (firstNum) + C (thirdNum) > B (secondNum)

B (secondNum) + C (thirdNum) > A (firstNum)

The program works fine if I use the following IF statement (see below), but the conditions make the statement a bit too long. I know I can also use nested IF statements but I'm wondering if there's a better way to do it.

if (firstNum+secondNum > thirdNum) && (firstNum+thirdNum > secondNum) && (secondNum+thirdNum > firstNum) {
    fmt.Println("A triangle can be built")
} else {
    fmt.Println("A triangle can't be built")
}

Thank you!

  • 写回答

1条回答 默认 最新

  • dsz7121 2019-06-12 23:24
    关注

    If you're worried about the line length you could simply split your if statement onto multiple lines to make it more readable:

    if     (firstNum  + secondNum > thirdNum)
        && (firstNum  + thirdNum  > secondNum)
        && (secondNum + thirdNum  > firstNum) {
        fmt.Println("A triangle can be built")
    } else {
        fmt.Println("A triangle can't be built")
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器