3 回答

TA貢獻(xiàn)2036條經(jīng)驗 獲得超8個贊
作為使用循環(huán)的替代方法,您可以使其成為遞歸函數(shù)。我不知道當(dāng)你顯然嘗試這樣做時,你做了什么,但這應(yīng)該按預(yù)期工作。
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貢獻(xiàn)1812條經(jīng)驗 獲得超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?;蛘撸梢允褂谩袄^續(xù)”。else if

TA貢獻(xiàn)1828條經(jīng)驗 獲得超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 關(guān)注
- 158 瀏覽
添加回答
舉報