mardi 26 décembre 2017

display three most often random numbers c#

Hy, I'm learning c# and I need some help with code. I'm doing two aplications, first aplication displaying 4 random number every second and writing them in txt file, second aplication must read numbers from txt file and show 3 most often displayed random numbers. I done the first aplication:

//my code


namespace Zreb
{
    class Program
    {
        static void Main(string[] args)
        {
            //timer 1 second
            Timer timer = new Timer(TimerCallback, null, 0, 1000);
            Console.ReadLine();     

        }
           //random numbers
            private static void TimerCallback(object o)
            {
            string filename = @"zreb.txt";
            Random rnd = new Random();
            int rndNumber=0;
            int rndNumber2=0;
            int rndNumber3=0;
            int rndNumber4=0;

            //  writing in file
            using (StreamWriter writer = new StreamWriter(filename, false, Encoding.UTF8))
            {
                for (int i = 1; i < 1500; i++)
                {
                    rndNumber = rnd.Next(1, 81);
                    rndNumber2 = rnd.Next(1, 81);
                    rndNumber3 = rnd.Next(1, 81);
                    rndNumber4 = rnd.Next(1, 81);

                    string num1 = rndNumber.ToString();
                    string num2 = rndNumber2.ToString();
                    string num3 = rndNumber3.ToString();
                    string num4 = rndNumber4.ToString();



                    //writing in text file 4 rnd num
                    writer.WriteLine("{0}, {1}, {2}, {3}", num1, num2, num3, num4);

                    GC.Collect();
                }
                    //writing in console
                  Console.WriteLine("{0}, {1}, {2}, {3}", rndNumber, rndNumber2, rndNumber3, rndNumber4);
                writer.Flush();
                writer.Close();
            }
        }

    }
 }

First aplication is every thing ok, but the second aplication i don't know how to compare numbers and show 3 most often...this is my code for second aplication...but i don't know how to continue....Please help me....

FileStream fs;
StreamReader srIn;

try
{
    fs = new FileStream(@"c:\\Users\bojan\source\repos\Zreb\Zreb\bin\Debug\zreb.txt", FileMode.Open);
    srIn = new StreamReader(fs);
    string line = srIn.ReadLine();

    while (line != null)
    {
        line = srIn.ReadLine();
        Console.WriteLine(line);              
}
srIn.Close();




Aucun commentaire:

Enregistrer un commentaire