有人可以回答我错过的东西吗? [英] Can somebody answer what I'm missing?

查看:75
本文介绍了有人可以回答我错过的东西吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望用户可以搜索true或false,以便用户可以知道boolVektor中保存了多少true或false。我必须使用convert.Toboolean。但我不知道怎么做。目前我正在得到这个东西"不能隐瞒转换类型
bool到bool []"

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");
                }
            }

推荐答案

您正在尝试将单个bool值分配给bool数组。你需要只分配一个数组元素。

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.
            }


这篇关于有人可以回答我错过的东西吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆