猿问

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

我已经在codereview.stackexchange上发布了这个,我认为那里更合适,但没有得到任何反馈,所以我把它发布在这里,那里可能有更多的观众。


我使用此代码进行了一些输入检查。在任何失败的检查中,我需要再次要求正确的输入。我使用并实现了这一点,他们似乎被程序员作为一个概念所不喜欢。labelsgoto


如何在没有标签/转到的情况下达到相同的效果?我考虑过将所有这些代码放在一个函数中并从内部调用自己,但由于某种原因,它只重复了一次 - 没有一直问是否一直得到错误的答案。


// 0 exits

var f float64

var n int

startGame := 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

    }

}


蝴蝶刀刀
浏览 134回答 3
3回答

慕桂英3389331

作为使用循环的替代方法,您可以使其成为递归函数。我不知道当你显然尝试这样做时,你做了什么,但这应该按预期工作。func startGame() {&nbsp; &nbsp; var f float64&nbsp; &nbsp; fmt.Println()&nbsp; &nbsp; fmt.Print("Give number (1-9): ")&nbsp; &nbsp; _, err := fmt.Scan(&f)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("Letters or symbols not accepted")&nbsp; &nbsp; &nbsp; &nbsp; startGame()&nbsp; &nbsp; }&nbsp; &nbsp; if f == 0 {&nbsp; &nbsp; &nbsp; &nbsp; os.Exit(0)&nbsp; &nbsp; }&nbsp; &nbsp; if f < 1 || f > 9 || f-math.Ceil(f) != 0 {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("Only integer numbers between 1-9 are accepted")&nbsp; &nbsp; &nbsp; &nbsp; startGame()&nbsp; &nbsp; }&nbsp; &nbsp; n := int(f)&nbsp; &nbsp; if myArray[n-1] == false {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("Empty cell", int(f))&nbsp; &nbsp; &nbsp; &nbsp; startGame()&nbsp; &nbsp; }}func main() {&nbsp; &nbsp; startGame()&nbsp; &nbsp; fmt.Println("good luck! bye.")}

ABOUTYOU

var f float64 = math.MaxFloat32var n intfor ;f!=0; {&nbsp; &nbsp; fmt.Println()&nbsp; &nbsp; fmt.Print(`Give number (1-9): `)&nbsp; &nbsp; _, err := fmt.Scan(&f)&nbsp; &nbsp; // check letters or symbols&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("Letters or symbols not accepted")&nbsp; &nbsp; &nbsp; &nbsp; continue&nbsp; &nbsp; }&nbsp; &nbsp; // check for integers only&nbsp; &nbsp; if f < 1 || f > 9 || f-math.Ceil(f) != 0 {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("Only integer numbers between 1-9 are accepted")&nbsp; &nbsp; &nbsp; &nbsp; continue&nbsp; &nbsp; }&nbsp; &nbsp; n = int(f)&nbsp; &nbsp; // check for empty cells&nbsp; &nbsp; if f > 0 && myArray[n-1] == false {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("Empty cell", n)&nbsp; &nbsp; }}此外,上述所有条件也可以作为s。或者,您可以使用“继续”。else if

慕田峪7331174

只是使用,如果evrything是好的。喜欢这个:for {}continuebreak&nbsp; &nbsp; startGame := func() {&nbsp; &nbsp; &nbsp; &nbsp; for {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Print(`Give number (1-9): `)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _, err := fmt.Scan(&f)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // check letters or symbols&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("Letters or symbols not accepted")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // exit&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if f == 0 {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; os.Exit(0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // check for integers only&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if f < 1 || f > 9 || f-math.Ceil(f) != 0 {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("Only integer numbers between 1-9 are accepted")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if !true {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("Empty cell", n)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; startGame()&nbsp; &nbsp; fmt.Println("good luck! bye.")
随时随地看视频慕课网APP

相关分类

Go
我要回答