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

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

问题描述

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





我的尝试:



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



What I have tried:

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[]"

推荐答案

你声明的 boolVektor 作为一个布尔数组,而不是一个布尔值。



在你的循环中,你有:

Your declared boolVektor as an array of booleans, not a single boolean.

In your loop, you've got:
for (int i = 0; i < boolVektor.Length; i++)
{
    int slump = newRandom.Next(0,2);
    if (slump == 0)
        boolVektor = true;
    else
        boolVektor = false; 
}



嗯......你不能为整个数组赋值。你必须在数组上使用索引:


Ummmm... you can't assign a value to the entire array. You have to use an index on the array:

for (int i = 0; i < boolVektor.Length; i++)
{
    int slump = newRandom.Next(0,2);
    if (slump == 0)
        boolVektor[i] = true;
}



您也不需要将值设置为false,因为它是布尔值的默认值。


You also don't need to set the value to false as it is the default value for a boolean.




我不完全是为了你希望用户搜索真假的目的。

但是更改下面的代码行得到错误将解决您的错误。



Hi,
I don't get exactly for what purpose you want your user to search for true and false.
But changing the following lines of code where you're getting error will resolve your error.

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





因为你是初学者,我试着为你清楚。



你知道什么是数组吗?我想你知道这就是你使用 进行循环的原因,因为我们在处理集合时通常会使用循环。数组是一些东西的集合。把它当成复数而不是单数。您为整个集合分配了一个值,在您的情况下,此集合可以是1到101之间的任意随机长度,因为您已经定义了它。



As you're beginner I give a try to make it clear for you.

Do you know what is an array?, I guess you know that's why you have used a for loop, because we use loops normally when we work on the collections. Array is a collection of something. Take it like a plural not a singular. You were assigning a single value to the whole collection, in your case this collection can be of any random length between 1 to 101, since you have defined it.

int slumpTal = newRandom.Next(1, 101);



现在当你试图一次为整个数组分配一个值时给你一个错误。尝试按照我向你展示的索引逐个分配值。

希望这会有所帮助,如果有任何混淆,我愿意接受更多讨论。

Happy学习!


Now when you were trying to assign a single value to the whole array at once it was giving you an error. Try assigning values one by one, index by index as I have shown you.
Hope this will help, if any confusion is left I am open for more discussions.
Happy learning!


<pre> Random newRandom = new Random();
            int slumpTal = newRandom.Next(1, 101);
            bool[] boolVektor = new bool[slumpTal];

            //var search = Convert.ToBoolean(Convert.ToInt32("0"));

            for (int i = 0; i < boolVektor.Length; i++)
            {
                int slump = newRandom.Next(0, 2);
                if (slump == 0)
                    boolVektor[i] = true;
                else
                    boolVektor[i] = 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");
                    }
                }
                Console.ReadLine();
            }
        }
    }


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

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