mercredi 18 avril 2018

C#: My Else statement gets caught up in the Array, how do I overlook it?

I am having trouble with this code, I want to have the code return an answer if the input matches an item in the list, (Can't use an array), or if there is no match, write the line once and repeat the code, but when I do this, the result is; "Please enter the type of gem you want on your wedding ring: diamond Im sorry that was not a valid selection. Im sorry that was not a valid selection. Im sorry that was not a valid selection. Your wedding ring will feature your selection: diamond Im sorry that was not a valid selection. Im sorry that was not a valid selection."

The code is;

List<string> Gems = new List<string>();
    Gems.Add("ruby");
    Gems.Add("emerald");
    Gems.Add("topaz");
    Gems.Add("diamond");
    Gems.Add("pearl");
    Gems.Add("infinity stone");
    Console.WriteLine("Please enter the type of gem you want on your wedding ring: ");
    string want = Console.ReadLine();
    for (int g = 0; g < Gems.Count; g++)
    {
        if (want == Gems[g])
        {
            Console.WriteLine("Your wedding ring will feature your selection: " + Gems[g]);
        }
        else 
        {
            Console.WriteLine("Im sorry that was not a valid selection.");               
        }

    }

Aucun commentaire:

Enregistrer un commentaire