Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Typing_Speed
- {
- class Stopwatch
- {
- private DateTime startTime;
- private DateTime stopTime;
- public double time;
- public void Start()
- {
- startTime = DateTime.Now;
- }
- public void Stop()
- {
- stopTime = DateTime.Now;
- TimeSpan TotalTime = stopTime - startTime;
- time = TotalTime.TotalSeconds;
- }
- public double Display()
- {
- return Math.Round(time, 2);
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- Stopwatch stopwatch = new Stopwatch();
- Random random = new Random();
- string[] sentences = new string[] {"The quick brown fox jumps over the lazy dog."};
- bool exit = false;
- while (exit == false)
- {
- Console.WriteLine("Write the following sentence:");
- Console.WriteLine("----------------------------------\n");
- string sentence = sentences[random.Next(0, sentences.Count())];
- Console.WriteLine(sentence);
- Console.WriteLine("Enter to start!");
- Console.ReadLine();
- stopwatch.Start();
- string input = Console.ReadLine();
- stopwatch.Stop();
- if (string.IsNullOrWhiteSpace(input))
- {
- exit = true;
- break;
- }
- else
- {
- string[] words = input.Trim().Split(' ');
- string[] sentenceRandom = sentence.Split(' ');
- int mistakes = 0;
- if (sentenceRandom.Count() > words.Count())
- {
- for (int i = 0; i < sentenceRandom.Count(); i++)
- {
- try
- {
- if (!(sentenceRandom[i].Equals(words[i])))
- {
- mistakes++;
- }
- }
- catch (IndexOutOfRangeException)
- {
- mistakes++;
- }
- }
- }
- else
- {
- for (int i = 0; i < words.Count(); i++)
- {
- try
- {
- if (!(sentenceRandom[i].Equals(words[i])))
- {
- mistakes++;
- }
- }
- catch (IndexOutOfRangeException)
- {
- mistakes++;
- }
- }
- }
- double speed = sentenceRandom.Count() / stopwatch.time;
- int characters = 0;
- foreach (char character in input)
- {
- characters++;
- }
- Console.WriteLine("\n---------------------------");
- Console.WriteLine("Mistakes made: {0}", mistakes);
- Console.WriteLine("Words typed: {0}", words.Count());
- Console.WriteLine("Characters typed: {0}", characters);
- Console.WriteLine("Words per second: {0}", Math.Round(speed, 1));
- Console.WriteLine("Characters per second: {0}", Math.Round(characters / stopwatch.time, 1));
- Console.WriteLine("---------------------------\n\n");
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment