为了账号安全,请及时绑定邮箱和手机立即绑定

多次输入检查,如果条件不满足,则重复

多次输入检查,如果条件不满足,则重复

Go
蝴蝶刀刀 2022-08-09 20:33:58
我已经在codereview.stackexchange上发布了这个,我认为那里更合适,但没有得到任何反馈,所以我把它发布在这里,那里可能有更多的观众。我使用此代码进行了一些输入检查。在任何失败的检查中,我需要再次要求正确的输入。我使用并实现了这一点,他们似乎被程序员作为一个概念所不喜欢。labelsgoto如何在没有标签/转到的情况下达到相同的效果?我考虑过将所有这些代码放在一个函数中并从内部调用自己,但由于某种原因,它只重复了一次 - 没有一直问是否一直得到错误的答案。// 0 exitsvar f float64var n intstartGame := func() {reception:    fmt.Println()    fmt.Print(`Give number (1-9): `)    _, err := fmt.Scan(&f)    // check letters or symbols    if err != nil {        fmt.Println("Letters or symbols not accepted")        goto reception    }    // exit    if f == 0 {        os.Exit(0)    }    // check for integers only    if f < 1 || f > 9 || f-math.Ceil(f) != 0 {        fmt.Println("Only integer numbers between 1-9 are accepted")        goto reception    }    n = int(f)    // check for empty cells    if myArray[n-1] == false {        fmt.Println("Empty cell", n)        goto reception    }}
查看完整描述

3 回答

?
慕桂英3389331

TA贡献2036条经验 获得超8个赞

作为使用循环的替代方法,您可以使其成为递归函数。我不知道当你显然尝试这样做时,你做了什么,但这应该按预期工作。


func startGame() {

    var f float64

    fmt.Println()

    fmt.Print("Give number (1-9): ")

    _, err := fmt.Scan(&f)


    if err != nil {

        fmt.Println("Letters or symbols not accepted")

        startGame()

    }

    if f == 0 {

        os.Exit(0)

    }

    if f < 1 || f > 9 || f-math.Ceil(f) != 0 {

        fmt.Println("Only integer numbers between 1-9 are accepted")

        startGame()

    }

    n := int(f)

    if myArray[n-1] == false {

        fmt.Println("Empty cell", int(f))

        startGame()

    }

}


func main() {

    startGame()

    fmt.Println("good luck! bye.")

}


查看完整回答
反对 回复 2022-08-09
?
ABOUTYOU

TA贡献1812条经验 获得超5个赞

var f float64 = math.MaxFloat32

var n int

for ;f!=0; {

    fmt.Println()

    fmt.Print(`Give number (1-9): `)

    _, err := fmt.Scan(&f)

    // check letters or symbols

    if err != nil {

        fmt.Println("Letters or symbols not accepted")

        continue

    }

    // check for integers only

    if f < 1 || f > 9 || f-math.Ceil(f) != 0 {

        fmt.Println("Only integer numbers between 1-9 are accepted")

        continue

    }


    n = int(f)

    // check for empty cells

    if f > 0 && myArray[n-1] == false {

        fmt.Println("Empty cell", n)

    }

}

此外,上述所有条件也可以作为s。或者,您可以使用“继续”。else if


查看完整回答
反对 回复 2022-08-09
?
慕田峪7331174

TA贡献1828条经验 获得超13个赞

只是使用,如果evrything是好的。喜欢这个:for {}continuebreak


    startGame := func() {

        for {

            fmt.Println()

            fmt.Print(`Give number (1-9): `)

            _, err := fmt.Scan(&f)


            // check letters or symbols

            if err != nil {

                fmt.Println("Letters or symbols not accepted")

                continue

            }


            // exit

            if f == 0 {

                os.Exit(0)

            }


            // check for integers only

            if f < 1 || f > 9 || f-math.Ceil(f) != 0 {

                fmt.Println("Only integer numbers between 1-9 are accepted")

                continue

            }


            if !true {

                fmt.Println("Empty cell", n)

                continue

            }

            break

        }

    }

    startGame()

    fmt.Println("good luck! bye.")


查看完整回答
反对 回复 2022-08-09
  • 3 回答
  • 0 关注
  • 133 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信