Always get the first value as false when looking for a number?

What I'm trying to do is let the user search for the number of values falseeither truein boolVector. It is also important that the program handles input errors.

I think I need to use it Convert.Boolean, but I don’t know how to do it. At the moment, I continue to receive the same, regardless of whether I am looking for numbers or letters.

static void Main(string[] args)
{
    Random newRandom = new Random();
    int slumpTal = newRandom.Next(1, 101);
    bool[] boolVektor = new bool[slumpTal];

    //var nacas = 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("The following were found: " + boolVektor[i]);
                search = true;
            }
            if (!search)
            {
                Console.WriteLine("Your search failed");
            }
        }
        Console.ReadLine();
    }
}
+4
source share
2 answers

To find the value of a specific data type in an array, you need to convert user input to this data type. Then you go over and compare the converted value in the same way as now.

The conversion can be performed as follows:

Console.Write("Skriv in sökOrd: [TRUE|FALSE]");
string searchWord = Console.ReadLine();
bool search = Convert.ToBoolean(searchWord);

bool foundAnyMatches = false


for (int i = 0; i < boolVektor.Length; i++)
{
    if (boolVektor[i] == search)
    {
        Console.WriteLine("The following were found: " + boolVektor[i] + 
        "Index: " + i);
        foundAnyMatches = true;
    }
}
if (!foundAnyMatches)
{
    Console.WriteLine("Your search failed");
}

search! !

EDIT:

, try/catch, Boolean.TryParse()

+4

:

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[ i ] = true;
    else
        boolVektor[ i ] = false;
}

Console.Write( "True/False: " );
bool search = Convert.ToBoolean(Console.ReadLine()); //Thanks Mong Zhu
bool foundMatches = false;

for ( int i = 0; i < boolVektor.Length; i++ )
{
    if ( boolVektor[ i ] == search )
    {
        //If you do boolVektor[i] it will just show true/false
        Console.WriteLine( $"The following index is found: {i} " );
        foundMatches = true;
    } 
}
if ( !foundMatches ) //We check if the search failed here because now the search is finished
{
    Console.WriteLine( "Your search failed" );
}
Console.ReadLine();

, for :

int count = boolVektor.Where( row => row == search ).Count();
if(count != 0)
{
    Console.WriteLine( $"{count} items were found" );
}
else
{
    Console.WriteLine( "Your search failed" );
}
Console.ReadLine();
+3

Source: https://habr.com/ru/post/1689671/


All Articles