用go写一段MBTI性格测试的代码
时间: 2024-05-10 17:15:44 浏览: 169
android 性格测试代码
5星 · 资源好评率100%
以下是一个简单的MBTI性格测试的代码示例,使用Go语言编写:
```go
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
func main() {
questions := []string{
"I prefer to spend time alone rather than socializing with a large group.",
"I rely on my intuition more than on my senses.",
"I like to plan things in advance rather than being spontaneous.",
"I prefer to have a few close friends rather than many acquaintances.",
"I prefer to work alone rather than in a team.",
"I value logic and reason more than emotions and feelings.",
"I like to have a routine rather than being flexible.",
"I prefer to focus on the big picture rather than the details.",
"I tend to be more reserved and quiet than outgoing and talkative.",
"I like to keep my options open rather than making a decision quickly.",
}
answers := make([]string, len(questions))
reader := bufio.NewReader(os.Stdin)
for i, q := range questions {
fmt.Printf("(%d) %s\n", i+1, q)
answer, _ := reader.ReadString('\n')
answer = strings.TrimSpace(answer)
answers[i] = answer
}
aCount := 0
tCount := 0
for _, a := range answers {
switch a {
case "A", "a":
aCount++
case "T", "t":
tCount++
default:
fmt.Println("Invalid answer: ", a)
}
}
var mbtiType string
if aCount > tCount {
mbtiType = "Introverted"
} else {
mbtiType = "Extroverted"
}
if answers[0] == "A" || answers[0] == "a" {
mbtiType += " Intuitive"
} else {
mbtiType += " Sensing"
}
if answers[2] == "A" || answers[2] == "a" {
mbtiType += " Judging"
} else {
mbtiType += " Perceiving"
}
fmt.Printf("Your MBTI type is %s\n", mbtiType)
}
```
该代码定义了一个问题数组和答案数组,通过循环询问问题并记录答案。然后,对答案数组进行计数,根据计数结果确定MBTI类型(根据A和T的数量)。最后,根据前三个答案,确定MBTI类型的第二个和第三个字母。
阅读全文