package main
import (
"bufio"
"fmt"
"math/rand"
"os"
"strings"
"time"
)
var phrases = []string{
"Practice makes perfect.",
"Go is an open source programming language.",
"Programming is thinking, not typing.",
"Hello, world! Welcome to Go programming.",
"Effort makes all things possible.",
"The only way to learn a new programming language is by writing programs in it.",
}
func main() {
// 随机选择一个短语
rand.Seed(time.Now().UnixNano())
phrase := phrases[rand.Intn(len(phrases))]
// 输出提示信息
fmt.Println("Type the following phrase as fast and accurately as you can:")
fmt.Println(phrase)
// 计时开始
startTime := time.Now()
// 获取用户输入
reader := bufio.NewReader(os.Stdin)
userInput, _ := reader.ReadString('\n')
// 清理输入的换行符
userInput = strings.TrimSpace(userInput)
// 计时结束
elapsedTime := time.Since(startTime)
// 计算正确率
correctCount := 0
for i := 0; i < len(phrase) && i < len(userInput); i++ {
if phrase[i] == userInput[i] {
correctCount++
}
}
accuracy := float64(correctCount) / float64(len(phrase)) * 100
// 输出结果
fmt.Printf("\nTime taken: %.2f seconds\n", elapsedTime.Seconds())
fmt.Printf("Accuracy: %.2f%%\n", accuracy)
if accuracy == 100 {
fmt.Println("Excellent! You typed it correctly.")
} else {
fmt.Println("Try again to improve your accuracy.")
}
}
02-08
444

05-25
494

07-30