【go】写一个命令行练打字的工具

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.")
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值