在我的 CLI 程序中,我有这个功能,用户可以告诉我他们想要多少特定产品的项目。因此,此输入应为整数。
我目前遇到一个问题,如果他们输入一个字符串,它会出错,expected an integer但随后还会再次打印多次而不是一次。
例子:
How many Fruit Tea would you like to buy? qwe
expected integer
How many Fruit Tea would you like to buy? expected integer
How many Fruit Tea would you like to buy? expected integer
How many Fruit Tea would you like to buy?
下面是处理此功能的代码
for {
fmt.Printf("How many %v would you like to buy? ", product.Name)
_, err := fmt.Scan(&response)
if err != nil {
fmt.Println(err)
continue
}
if ok, err := validResponse(response); ok {
break
} else {
fmt.Println(err)
continue
}
}
在这个循环中我必须改变什么,所以它只重复一次问题?
千巷猫影
相关分类