mercredi 20 janvier 2021

A problem that I tried to solve with the help of strings and repetitive instructions, but on the console the result is not what I expected

using System;

class Program { static void Main(string[] args)

{

    string candidateName = Console.ReadLine();

    string input = Console.ReadLine();

    int numberOfAdmittedPersons = Convert.ToInt32(input);

    string result = string.Empty;


    for (int i = 1; i <= numberOfAdmittedPersons; i++)

    {
        string listOfAllAdmittedPersons = Console.ReadLine();

        {

            if (i != numberOfAdmittedPersons)

            {

 result += listOfAllAdmittedPersons;


            }

        }

    }

            if (result.Contains(candidateName))


            {

                Console.WriteLine("true");

            }

            else

            {
                Console.WriteLine("false");
            }
        }
    }

The application receives on the first line the name of a candidate C (inputted by the user). The next line contains the number of persons admitted (also inputted by the user).Then the list of all admitted persons follows, one person per line (also inputted by the user).

I have to make an application which displays "True" if candidate C was admitted (C is found in the list) or "False" if the candidate was rejected (C is not on the list) for example : if I input :

John 3 George Maria John

The console will display : "True"

But the result I get is : "False".

What can I do to correct my code?

Aucun commentaire:

Enregistrer un commentaire