菜鸟求教,一个简单无限循环代码,但为什么输入bye,却无法结束呢?
package main
import(
"bufio"
"fmt"
"os"
"strings"
)
func main(){
inputReader := bufio.NewReader(os.Stdin)
fmt.Println("Please input your name:")
input,err := inputReader.ReadString('\n')
if err!=nil{
fmt.Printf("An error occurred :%s\n", err)
os.Exit(1)
}else{
name := input[:len(input)-1]
fmt.Printf("hello,%s What can i do for you!", name)
}
for {
input,err = inputReader.ReadString('\n')
if err!=nil{
fmt.Printf("An error occurred :%s\n", err)
continue
}
input = input[:len(input)-1]
input = strings.ToLower(input)
switch input{
case "":
continue
case "bye":
fmt.Println("bye!")
os.Exit(0)
default:
fmt.Printf("Sorry,I didn't catch you.")
}
}
}
有疑问加站长微信联系(非本文作者)