我试图让这段代码运行,但他跳过了功能登录package mainimport "fmt"var name stringvar password stringfunc getName() { fmt.Print("What's your name : \n") fmt.Scanf("%s", &name)}func showName() { fmt.Print("Your name is " + name + "\n")}func getPassword() { fmt.Print("What's your password : \n") fmt.Scanf("%s \n", &password)}func singOrLog() { var input string input = "" var loop bool loop = false for loop == false { fmt.Print("Do you want to make an account or login (0 for register and 1 login) : ") fmt.Scanf("%s \n", &input) if input == "0" { register() loop = true } else if input == "1" { login() loop = true } }}func login() { var nameCorrect string nameCorrect = name for !(nameCorrect == name) { fmt.Print("What's your name :\n") fmt.Scanf("%s", &nameCorrect) }}func register() { getName() showName() getPassword() login()}func main() { singOrLog()}
1 回答
慕的地10843
TA贡献1785条经验 获得超8个赞
登录功能不会被跳过,尽管它永远不会打印出任何东西。该行:
nameCorrect = name
将 nameCorrect 设置为与 name 相同,后跟:
for !(nameCorrect == name) {
仅当 name 和 nameCorrect 不同时才会执行。您可以通过添加以下行来查看:
fmt.Printf("Name: '%s', nameCorrect: '%s', !(nameCorrect == name): %t\n", name, nameCorrect, !(nameCorrect == name))
这将打印:
Name: '', nameCorrect: '', !(nameCorrect == name): false
我想你不想有线nameCorrect = name
。
- 1 回答
- 0 关注
- 62 浏览
添加回答
举报
0/150
提交
取消