3 回答
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.")
}
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
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.")
- 3 回答
- 0 关注
- 133 浏览
添加回答
举报