From 5c52f69e2f90cffb5cb0ad7cf1afc7e81e7eaa2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Sch=C3=BCler?= Date: Tue, 14 Oct 2025 13:07:27 +0200 Subject: [PATCH] E22 Ex1-5 --- Exercises/E22_Dictionary/Exercise_1.cs | 2 +- Exercises/E22_Dictionary/Exercise_2.cs | 5 ++-- Exercises/E22_Dictionary/Exercise_3.cs | 3 +- Exercises/E22_Dictionary/Exercise_4.cs | 10 ++++++- Exercises/E22_Dictionary/Exercise_5.cs | 38 +++++++++++++++++++++++++- lastPoint | 4 +-- 6 files changed, 54 insertions(+), 8 deletions(-) diff --git a/Exercises/E22_Dictionary/Exercise_1.cs b/Exercises/E22_Dictionary/Exercise_1.cs index 53a44c2..287469f 100644 --- a/Exercises/E22_Dictionary/Exercise_1.cs +++ b/Exercises/E22_Dictionary/Exercise_1.cs @@ -10,7 +10,7 @@ namespace Exercises_C_Sharp.E22_Dictionary public static void Start() { //Code Start - dynamic dic = -1; + Dictionary dic = []; //Code ENDE dic.Add("Hallo", 12.54); dic.Add("Hi", 7.33); diff --git a/Exercises/E22_Dictionary/Exercise_2.cs b/Exercises/E22_Dictionary/Exercise_2.cs index 2703178..47109d9 100644 --- a/Exercises/E22_Dictionary/Exercise_2.cs +++ b/Exercises/E22_Dictionary/Exercise_2.cs @@ -9,10 +9,11 @@ namespace Exercises_C_Sharp.E22_Dictionary //Geben Sie alle Value-Werte hintereinander mit Leerzeichen getrennt auf der Konsole aus. public static void Start() { - Dictionary dic = new() { {12, "Dies"}, {4, "ist"}, {22, "ein"}, {94, "tolles"}, {26, "Ding!"} }; + Dictionary dic = new() { { 12, "Dies" }, { 4, "ist" }, { 22, "ein" }, { 94, "tolles" }, { 26, "Ding!" } }; //Code Start - + foreach (var element in dic) + Console.Write(element.Value + " "); //Code ENDE } diff --git a/Exercises/E22_Dictionary/Exercise_3.cs b/Exercises/E22_Dictionary/Exercise_3.cs index f213738..2ef52d1 100644 --- a/Exercises/E22_Dictionary/Exercise_3.cs +++ b/Exercises/E22_Dictionary/Exercise_3.cs @@ -12,7 +12,8 @@ namespace Exercises_C_Sharp.E22_Dictionary Dictionary dic = new(); //Code Start - + dic["dieses"] = "Dies ist mein Programm"; + dic["ist"] = dic["Programm"] = dic["es"] = ""; //Code ENDE Console.Write(dic["dieses"]); diff --git a/Exercises/E22_Dictionary/Exercise_4.cs b/Exercises/E22_Dictionary/Exercise_4.cs index a05ed7c..bc6b6c4 100644 --- a/Exercises/E22_Dictionary/Exercise_4.cs +++ b/Exercises/E22_Dictionary/Exercise_4.cs @@ -9,10 +9,18 @@ namespace Exercises_C_Sharp.E22_Dictionary //Addieren Sie alle Value-Werte mit geraden Index auf und geben Sie das Ergebnis aus. public static void Start() { - Dictionary dic = new() {{1, 14.85}, {2, 58.52}, {3, 8.63}, {4, 6.49}, {5, 62.85}, {6, 2.22}, {7, 3.33}, {8, 9.99}}; + Dictionary dic = new() { { 1, 14.85 }, { 2, 58.52 }, { 3, 8.63 }, { 4, 6.49 }, { 5, 62.85 }, { 6, 2.22 }, { 7, 3.33 }, { 8, 9.99 } }; //Code Start + Console.WriteLine("Ergebnis: " + dic.Sum(k => k.Key % 2 == 0 ? k.Value : 0)); + + double sum = 0; + foreach (var element in dic) + if (element.Key % 2 == 0) + sum += element.Value; + Console.WriteLine("Ergebnis: " + sum); + //Code ENDE diff --git a/Exercises/E22_Dictionary/Exercise_5.cs b/Exercises/E22_Dictionary/Exercise_5.cs index 10993cf..d95d033 100644 --- a/Exercises/E22_Dictionary/Exercise_5.cs +++ b/Exercises/E22_Dictionary/Exercise_5.cs @@ -9,9 +9,45 @@ namespace Exercises_C_Sharp.E22_Dictionary //Der User soll hier ein Wort eingeben. Wenn es sich schon im Dictionary befindet, dann soll die Übersetzung ausgegeben werden. Wenn nicht, dann soll der User die Übersetzung eingeben und beide Elemente sollen in dem Dictionary gespeichert werden. Groß- und Kleinschreibung soll keine Rolle spielen. public static void Start() { - Dictionary dic = new(){{"black", "schwarz"}, {"exception", "Ausnahme"}, {"barrel", "Fass"}}; + Dictionary dic = new() { { "black", "schwarz" }, { "exception", "Ausnahme" }, { "barrel", "Fass" } }; //Code Start + while (true) + { + string userinput = (Console.ReadLine() ?? string.Empty).ToLower(); +/* + bool b = true; + foreach (var element in dic) + { + if (element.Key == userinput) + { + b = false; + //Ausgabe + Console.WriteLine("Übersetzung: " + element.Value); + } + } + if (b) + { + //Hinzufügen + Console.WriteLine("Leider ist das Wort nicht vorhanden. Bitte geben Sie die Übersetzung ein:"); + string translation = Console.ReadLine() ?? ""; + dic[userinput] = translation; + } +*/ + if (dic.ContainsKey(userinput)) + { + //Ausgabe + Console.WriteLine("Übersetzung: " + dic[userinput]); + } + else + { + //Hinzufügen + Console.WriteLine("Leider ist das Wort nicht vorhanden. Bitte geben Sie die Übersetzung ein:"); + string translation = Console.ReadLine() ?? ""; + dic[userinput] = translation; + + } + } //Code ENDE diff --git a/lastPoint b/lastPoint index 3bb7897..7abc138 100644 --- a/lastPoint +++ b/lastPoint @@ -1,2 +1,2 @@ -12 -19 +4 +21