samedi 3 mars 2018

how to solve do-while condition issues in C#?

I'm a beginner in c# and I have been assigned the following problem: Write a program that recieves an array of 8 positive integers, then recieves a number from the user and then calculates the probabillity of that number being in the array. this should continue until we input -1. for example this is my string:1,1,1,2,2,3,4,5 and then I input 2, so it should return ./25 this is how the input and output should look like:

input:

1,2,3,4,5,6,7,8

1

2

3(the inputs untill we enter -1)

-1

output:

./125

./125

./125

end (for -1)

so far, this is my code:

Console.WriteLine("enter 8 int values");

        string line = Console.ReadLine();
        string[] sps = line.Split(new char[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries);
        int[] myint = new int[8];
        myint = Array.ConvertAll(sps, int.Parse);

        int b = Convert.ToInt32(Console.ReadLine());
        int a;

        int[] yep = new int[8];
        do
        {
            Console.WriteLine("enter a number");
            a = Convert.ToInt32(Console.ReadLine());
            for (int j = 0; j < yep.Length; j++)
            {
                yep[j] = a;
            }


        } while (a != -1);

        if (a == -1)
        {
            int count = 0;
            for (int i = 0; i < myint.Length; i++)
            {


                for (int j = 0; j < yep.Length; j++)
                {
                    Console.WriteLine(yep[j]);

                    if (yep[j] == myint[i])
                    {
                        Console.WriteLine((double)count / (double)myint.Length);
                        count++;
                    }
                 else
                        Console.WriteLine(Convert.ToInt32(0));
                }
            }
        }         

I have recieved the string of numbers, converted it into an int array, then I recieved the integer from the user , then I did the algotithm for the output; first I determined that untill we enter -1; the inputs must be stored in an array and then when a==-1 we will move to if where the condition is checked for existance of the inputs in the first string(which we had already converted into an array at the beginning.) now there is something wrong whith this loops. they dont give me the right answer(the output is 0 -1 0 -1 constantly) and I also dont know how to declare the "end" part. I know that this is a very basic question but I would be glad if you help.

Aucun commentaire:

Enregistrer un commentaire