E22 Ex1-5

This commit is contained in:
Sebastian Schüler 2025-10-14 13:07:27 +02:00
parent df7a58d7c3
commit 5c52f69e2f
6 changed files with 54 additions and 8 deletions

View File

@ -10,7 +10,7 @@ namespace Exercises_C_Sharp.E22_Dictionary
public static void Start()
{
//Code Start
dynamic dic = -1;
Dictionary<string, double> dic = [];
//Code ENDE
dic.Add("Hallo", 12.54);
dic.Add("Hi", 7.33);

View File

@ -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<int, string> dic = new() { {12, "Dies"}, {4, "ist"}, {22, "ein"}, {94, "tolles"}, {26, "Ding!"} };
Dictionary<int, string> 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
}

View File

@ -12,7 +12,8 @@ namespace Exercises_C_Sharp.E22_Dictionary
Dictionary<string, string> dic = new();
//Code Start
dic["dieses"] = "Dies ist mein Programm";
dic["ist"] = dic["Programm"] = dic["es"] = "";
//Code ENDE
Console.Write(dic["dieses"]);

View File

@ -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<int, double> 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<int, double> 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

View File

@ -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<string, string> dic = new(){{"black", "schwarz"}, {"exception", "Ausnahme"}, {"barrel", "Fass"}};
Dictionary<string, string> 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

View File

@ -1,2 +1,2 @@
12
19
4
21