none
Can somebody answer what I'm missing? RRS feed

  • Question

  • I want that the user could search true or false, so that the user could know how many true or false is saved in the boolVektor. I have to use convert.Toboolean. But I don't know how. At the moment I'm getting this thing "Cannot implicity convert type bool to bool[]"

     static void Main(string[] args)
            {
                Random newRandom = new Random();
                int slumpTal = newRandom.Next(1, 101);
                bool[] boolVektor = new bool[slumpTal];
                
                for (int i = 0; i < boolVektor.Length; i++)
                {
                    int slump = newRandom.Next(0,2);
                    if (slump == 0)
                        boolVektor = true;
                    else
                        boolVektor = false; 
                }
                
                Console.Write("Skriv in sökOrd");
                string searchWord = Console.ReadLine();
                bool search = false;
                for (int i = 0; i < boolVektor.Length; i++)
                {
                    if (boolVektor[i] == search)
                    {
                        Console.WriteLine("Följande hittades" + boolVektor[i]);
                        search = true;
                    }
                    if (!search)
                    {
                        Console.WriteLine("Din sökning misslyckades");
                    }
                }

    Tuesday, November 21, 2017 11:10 PM

All replies

  • You are trying to assign a single bool value to an array of bool. You need to assign to just one element of the array.

                for (int i = 0; i < boolVektor.Length; i++)
                {
                    int slump = newRandom.Next(0,2);
                    if (slump == 0)
                        boolVektor[i] = true; // You forgot the [i] here.
                    else
                        boolVektor[i] = false; // And here.
                }

    • Proposed as answer by Fei HuModerator Wednesday, November 22, 2017 8:27 AM
    Wednesday, November 22, 2017 12:47 AM