我写了一个简单的 go 程序,但它不能正常工作:
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
reader := bufio.NewReader(os.Stdin)
fmt.Print("Who are you? \n Enter your name: ")
text, _ := reader.ReadString('\n')
if aliceOrBob(text) {
fmt.Printf("Hello, ", text)
} else {
fmt.Printf("You're not allowed in here! Get OUT!!")
}
}
func aliceOrBob(text string) bool {
if text == "Alice" {
return true
} else if text == "Bob" {
return true
} else {
return false
}
}
它应该要求用户说出它的名字,如果他是 Alice 或 Bob,请向他打招呼,否则告诉他离开。问题是,即使输入的名字是 Alice 或 Bob,它也会告诉用户离开。
爱丽丝:
/usr/lib/golang/bin/go run /home/jcgruenhage/go/workspace/src/github.com/jcgruenhage/helloworld/greet/greet.go
Who are you?
Enter your name: Alice
You're not allowed in here! Get OUT!!
Process finished with exit code 0
鲍勃:
/usr/lib/golang/bin/go run /home/jcgruenhage/go/workspace/src/github.com/jcgruenhage/helloworld/greet/greet.go
Who are you?
Enter your name: Bob
You're not allowed in here! Get OUT!!
Process finished with exit code 0
眼眸繁星
繁星coding
慕哥9229398
相关分类