Laden...

Forenbeiträge von Myrdrox Ingesamt 1 Beiträge

07.12.2021 - 17:11 Uhr

Hallöchen,

ich habe gerade angefangen mich mit Programmieren vertraut zu machen.
Als erste Programmiersprache habe ich mich für C# entschieden. Nachdem ich nun Unterrichte/Lehrvideos zu den Grundlagen der Grundlagen durchgearbeitet habe, möchte ich das Gelernte ein par mal Anwenden bevor ich weiter mache.

Dazu habe ich als erstes Projekt einen Währungsrechner ( Faktoren waren zur Zeit des Erstellens aktuell) geschrieben.
Gerade zu der Methode "Exchange" würde mich interessieren, welche elegantere Lösung es für das Problem gegeben hätte.
Im Speziellen, ob es eine Schleife gibt, mit welcher man das Problem hätte angehen können. Ich selber bin auf keine passende Schleife gekommen.
Gelernt habe ich bis dato: while, dowhile, for und foreach.


namespace Währungsrechner
{
    class Program
    {
        static void Main()
        {

            MainMenu();
        }
        static void MainMenu()
        {
            Console.WriteLine("Gebe einen Geldbetrag ein: ");
            decimal amount =Convert.ToDecimal(Console.ReadLine());
            Console.WriteLine();

            Console.WriteLine("Wähle die Währung");
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("[1] Euro");
            Console.WriteLine("[2] US-Dollar");
            Console.WriteLine("[3] Britische Pfund");
            Console.ResetColor();
            Console.Write("Auswahl: ");
            int input  = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine();

            Console.WriteLine("Wähle die gewünschte Währung:");
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("[1] Euro");
            Console.WriteLine("[2] US-Dollar");
            Console.WriteLine("[3] Britische Pfund");
            Console.ResetColor();
            Console.Write("Auswahl: ");
            int output = Convert.ToInt32(Console.ReadLine());

            Console.Clear();

            Exchange(amount, input, output);
            
        }
        static void Exchange(decimal amount, int input, int output)
        {
            decimal eurUsd = 1.12M;
            decimal eurGbp = 85.05M;
            decimal usdEur = 0.89M;
            decimal usdGbp = 75.69M;
            decimal gbpEur = 1.18M;
            decimal gbpUsd = 1.32M;

            if (input == output )
            {
                Console.WriteLine("Du hast die selben Währungen ausgewählt!");
            }
            else if (input == 1 && output == 2)
            {
                Console.WriteLine("{0} Euro entsprechen {1} Dollar", amount,  amount * eurUsd);
            }
            else if (input == 1 && output == 3)
            {
                Console.WriteLine("{0} Euro entsprechen {1} Britischen Pfund", amount,  amount * eurGbp);
            }
            else if (input == 2 && output == 1)
            {
                Console.WriteLine("{0} US-Dollar entsprechen {1} Euro", amount, amount * usdEur);
            }
            else if (input == 2 && output == 3)
            {
                Console.WriteLine("{0} US-Dollar entsprechen {1} Britischen Pfund", amount, amount * usdGbp);
            }
            else if (input == 3 && output == 1)
            {
                Console.WriteLine("{0} Britische Pfund entsprechen {1} Euro", amount, amount * gbpEur);
            }
            else if (input == 3 && output == 2)
            {
                Console.WriteLine("{0} Britische Pfund entsprechen {1} US-Dollar", amount, input * gbpUsd);
            }
            Console.ReadKey();
            Console.Clear();
        }
        
    }
}