C# :: Aufgabe #262 :: Lösung #1

8 Lösungen Lösungen öffentlich
#262

Array von Zahlen in die nächstgelegene durch 5 teilbare Zahl umwandeln

Anfänger - C# von Gustl - 08.05.2019 um 20:08 Uhr
Schreibe ein Programm welches aus einem Array von Dezimalzahlen diese Zahlen in die nächstgelegene durch 5 teilbare Zahl umwandeln.

Etwa so:

Konsolenausgabe:

 7.1 => 5
8.4 => 10
-2.4 => 0
-2.6 => -5
-8.3 => -10
#1
2x
vote_ok
von hollst (13980 Punkte) - 09.05.2019 um 09:11 Uhr
Quellcode ausblenden C#-Code
using System;
using static System.Console;
using System.Collections.Generic;

namespace Aufgabe_262 //Array von (reellen) Zahlen in die (jeweils) nächstgelegene (restlos) durch 5 teilbare Zahl umwandeln    

{
    class Program
    {
        static void Main()
        {
            bool bo_loop = true;
            Random rand = new Random();
            double xmax = 1000.0;
            List<double> l_double = new List<double>();
            List<int> l_int = new List<int>();

            while (bo_loop)
            {
                double x = xmax * (rand.NextDouble() - 0.5);
                l_double.Add(x);
                int i = (int)(5.0 * Math.Round(x / 5.0));
                l_int.Add(i);

                WriteLine($"{x.ToString("0.0000"), 10} -> {i, 5}       for next double press any key (ESC for exit)");

                ConsoleKeyInfo ki = ReadKey(true);
                bo_loop = ki.Key != ConsoleKey.Escape;
            }

            double[] a_double = l_double.ToArray();
            int[] a_int = l_int.ToArray();

            WriteLine("ready");
            ReadKey();
        }
    }
}

Kommentare:

Für diese Lösung gibt es noch keinen Kommentar

Bitte melden Sie sich an um eine Kommentar zu schreiben.
Kommentar schreiben