dongzhi7763 2017-08-15 11:35
浏览 39
已采纳

为什么Go编程中没有while循环? 试图让用户输入数字1到12? [关闭]

package main
import (
    "fmt" 
)

func main() {
    var intN, intCounter int32
    var ulngFact int32

    for {
        fmt.Println("Enter a number (0 < N <= 12): ")//A do while loop
        fmt.Scanf("%d", &intN)                      // Until user enters
        if intN > 0 && intN < 12 {                  // a num 1 through 12
                break;
        }
    }
    ulngFact = 1
    for intCounter = 1; intCounter <= intN; intCounter++{
            ulngFact = ulngFact * intCounter
    }

    fmt.Printf("Required factorial is %d
", ulngFact)
    fmt.Printf("Thank you.
") }
  • 写回答

1条回答 默认 最新

  • dongsong8932 2017-08-16 00:53
    关注

    Here is your "while" loop. Go simply doesn't have a while loop because it's for loop already covers that.

    for !(intN > 0 && intN <= 12) {
        fmt.Println("Enter a number (0 < N <= 12): ")
        fmt.Scanf("%d", &intN)
    }
    

    Not sure if you wanted the 12 included or not (the print statement suggests it should be included, so I did include it).

    Hope this helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部