C# :: Aufgabe #48
6 Lösungen

Stoppuhr mit Rundenzeiten
Anfänger - C#
von Dome
- 10.03.2013 um 23:36 Uhr
Erstellen Sie ein Programm, womit man die Zeit stoppen kann. Die Stoppuhr sollte mit einer beliebigen Taste anfangen die Zeit zu starten und danach mit einer beliebigen Taste eine weitere Runde einzuleiten. Die Stoppuhr sollte mit dem Druck auf ESC beendet werden.
Lösungen:

static void Main(string[] args) { var list = new List<TimeSpan>(); Console.Write("Stoppuhr mit beliebiger Taste starten: "); Stopwatch sw = new Stopwatch(); while (Console.ReadKey().Key != ConsoleKey.Escape) { Console.Write("\nNeue Runde mit beliebiger Taste: "); list.Add(sw.Elapsed); sw.Restart(); } sw.Stop(); list.Add(sw.Elapsed); Console.WriteLine(); for (int i = 1; i < list.Count; i++) { Console.WriteLine("Runde {0}: {1}", i, list[i]); } Console.WriteLine("Gesamt: {0}", new TimeSpan(list.Sum(t => t.Ticks))); Console.ReadKey(); }
Hier mal eine Lösung mit GUI...
Mit Space wird die Uhr gestartet oder gestoppt, mit c zurückgesetzt, mit v wird die Rundenzeit ermittelt und mit ESC wird das Programm beendet
Sreenshot
C#-Code
Mit Space wird die Uhr gestartet oder gestoppt, mit c zurückgesetzt, mit v wird die Rundenzeit ermittelt und mit ESC wird das Programm beendet
Sreenshot

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Diagnostics; namespace Stopuhr { public partial class Form1 : Form { Stopwatch sw; int round = 1; TimeSpan abrechnen = new TimeSpan(0, 0, 0, 0, 0); public Form1() { InitializeComponent(); sw = new Stopwatch(); lblZeit.Text = "Time: 00:00:00"; dataGridView1.Visible = false; } private void tUpdate_Tick(object sender, EventArgs e) { lblZeit.Text = "Time: " + sw.Elapsed.ToString("mm\\:ss\\.ff"); } private void Form1_KeyDown(object sender, KeyEventArgs e) { switch (e.KeyCode) { case Keys.Space: { if (!(sw.IsRunning)) { sw.Start(); tUpdate.Enabled = true; lblZeit.Text = "Time: " + sw.Elapsed.ToString("mm\\:ss\\.ff"); } else { sw.Stop(); tUpdate.Enabled = false; } break; } case Keys.C: { sw.Reset(); lblZeit.Text = "Time: 00:00:00"; dataGridView1.Rows.Clear(); break; } case Keys.V: { dataGridView1.Visible = true; string etime = (sw.Elapsed.Subtract(abrechnen)).ToString("mm\\:ss\\.ff"); abrechnen = (TimeSpan)sw.Elapsed; dataGridView1.Rows.Add(); dataGridView1[0, round - 1].Value = round.ToString(); dataGridView1[1, round - 1].Value = etime; round++; break; } case Keys.Escape: { Application.Exit(); break; } } } } }

using System; using System.Windows.Forms; namespace Stoppuhr { public partial class stoppuhr : Form { DateTime temp; public stoppuhr() { InitializeComponent(); } private void timer1_Tick(object sender, EventArgs e) { textBox1.Text = DateTime.Now.Subtract(temp).ToString(); } private void stoppuhr_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Escape) System.Environment.Exit(0); else { if (temp != DateTime.MinValue) textBox2.Text = DateTime.Now.Subtract(temp).ToString(); temp = DateTime.Now; timer1.Start(); } } } }
Ich bin noch relativ frisch mit dem Programmieren und habe hier schon ein paar der Aufgaben gemacht. Aber auf diese Lösung bin ich besonders stolz, weil die Zeit live auf der Konsole ausgegeben wird und das war für mich eine echte Kopfnuss! Das Programm lastet die CPU ziemlich stark aus, weil ständig eine Schleife läuft. Das weiss ich (noch) nicht, wie man das besser macht, würde mich aber über konstruktive Kritik freuen! Die Kommentare sind auf Englisch, weil ich das einfach schöner finde und auch so gewohnt bin.
Und wenn jemand Fragen zum Code hat, einfach fragen. :)
C#-Code
Und wenn jemand Fragen zum Code hat, einfach fragen. :)

using System; using System.Diagnostics; namespace Timer { internal class Program { private static void Main(string[] args) { Stopwatch Time = new Stopwatch(); ConsoleKeyInfo PressedKey = new ConsoleKeyInfo(); int CountMyRound = 1; Console.WriteLine( "Press any key, to start timer." ); PressedKey = Console.ReadKey(); Time.Start(); do//bee doo { Console.WriteLine( "Round {0} started", CountMyRound.ToString() ); Console.WriteLine( "Press any key, to start another round. (Press 'Escape' to exit)" ); /* this while-loop is executed, until a Key is pressed * it puts the passed time out on the console with a following carriage return */ while (!Console.KeyAvailable) { TimeSpan ElapsedTime = Time.Elapsed; string ElapsedTimeString = String.Format( "Round {0}: {1:00}:{2:00}:{3:00}:{4:000}", CountMyRound.ToString(), ElapsedTime.Hours, ElapsedTime.Minutes, ElapsedTime.Seconds, ElapsedTime.Milliseconds ); Console.Write( ElapsedTimeString + "\r" ); } PressedKey = Console.ReadKey(); Console.WriteLine( "" ); if (PressedKey.Key == ConsoleKey.Escape) break; else { Time.Stop(); CountMyRound++; Time.Restart(); } } while (PressedKey.Key != ConsoleKey.Escape); } } }

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics; using System.Threading.Tasks; namespace TrainYourProgrammer48 { class Program { static void Main(string[] args) { bool stoppuhrLaeuft = true; int runde = 1; TimeSpan gesamtZeit = new TimeSpan(0,0,0); Stopwatch stoppuhr = new Stopwatch(); Console.WriteLine("Mit beliebiger Taste startet die Stoppuhr."); Console.WriteLine("Während die Stoppuhr läuft kann mit beliebiger Taste eine Rundenzeit genommen werden."); Console.WriteLine("Das drücken von ESC beendet die Stoppuhr."); Console.ReadKey(); Console.CursorLeft--; Console.WriteLine(" "); stoppuhr.Start(); Console.WriteLine("Stoppuhr läuft."); while (stoppuhrLaeuft) { ConsoleKeyInfo taste = Console.ReadKey(); if (taste != null && taste.Key != ConsoleKey.Escape) { Console.WriteLine("Runde {0}: {1}", runde, stoppuhr.Elapsed); gesamtZeit += stoppuhr.Elapsed; stoppuhr.Restart(); runde++; } else if (taste.Key == ConsoleKey.Escape) { Console.CursorLeft--; Console.WriteLine("Runde {0}: {1}", runde, stoppuhr.Elapsed); gesamtZeit += stoppuhr.Elapsed; Console.WriteLine("\nGesamtzeit: {0}\n", gesamtZeit); stoppuhrLaeuft = false; stoppuhr.Reset(); Console.WriteLine("Stoppuhr gestoppt.\nProgramm beendet auf Tastendruck."); } } Console.ReadKey(); } } }

static void Aufgabe_48() { DateTime rundenzeit, startzeit = DateTime.Now; ConsoleKeyInfo key = new ConsoleKeyInfo(); int top = 2, counter = 1; Console.WriteLine("Stoppuhr starten? (Beliebige Taste zum starten und neue Runde / ESC zum beenden)"); while (key.Key != ConsoleKey.Escape) { rundenzeit = DateTime.Now; while (!Console.KeyAvailable) { Thread.Sleep(1); Console.SetCursorPosition(0, 1); Console.Write("Gesamtzeit: {0} Rundenzeit: {1}", DateTime.Now.TimeOfDay - startzeit.TimeOfDay, DateTime.Now.TimeOfDay - rundenzeit.TimeOfDay); } key = Console.ReadKey(); if (key.Key != ConsoleKey.Escape) { Console.SetCursorPosition(0, top); Console.WriteLine("{0}. {1}", counter, DateTime.Now.TimeOfDay - rundenzeit.TimeOfDay); top++; counter++; } } Console.WriteLine("Stoppuhr erfolgreich beendet."); }