samedi 30 novembre 2019

How do I program the following contingency? C#

I have an array with names of Pets and another parallel array with the names of the sounds they make. I'm writing a method ShowSounds which asks user for an animal name, and then displays the respective sound. How do I code in an error message that says "Sorry that animal isn't in our list" if the user enters something random? The problem I have right now is that with the if statement it displays the error message 4 times even if I enter the correct animal.

public static void ShowSound(string userInput2, string[] localPets4, string[] localSounds2)
        {
            for (int l = 0; l < localPets4.Length; l++)
            {
                if (userInput2 == localPets4[l])
                {
                    Console.WriteLine("{0} makes the sound {1}", localPets4[l], localSounds2[l]);
                    Console.WriteLine();
                }
                else
                {
                    Console.WriteLine("Sorry that item isn't in our list of animals");
                }

            }
        }

Aucun commentaire:

Enregistrer un commentaire