dimanche 11 octobre 2020

How do I go through the if, else statement again after making a change in one of the else ifs?

Here is my code. In if (currSum > flowersForOneWreath) I make currLiliesNum -= 2; and I want to go back to the if else statement and check again the currSum vs flowersForOneWreath, but in this case I go out of the while loop and no result is printed on the console. Any ideas? I tried to put continue; after currLiliesNum -= 2;, but nothing changed.

var liliesInput = Console.ReadLine()
                .Split(", ", StringSplitOptions.RemoveEmptyEntries)
                .Select(int.Parse)
                .ToArray();
            var rosesInput = Console.ReadLine()
                .Split(", ", StringSplitOptions.RemoveEmptyEntries)
                .Select(int.Parse)
                .ToArray();

            var liliesStack = new Stack<int>(liliesInput);
            var rosesStack = new Stack<int>(rosesInput.Reverse());

            var flowersForOneWreath = 15;
            var storedFlowersForLater = 0;
            var wreathsCount = 0;
            var wreathsNeeded = 5;

            while (liliesStack.Any())
            {
                var currLiliesNum = liliesStack.Peek();
                var currRosesNum = rosesStack.Peek();
                var currSum = currLiliesNum + currRosesNum;

                if (currSum > flowersForOneWreath)
                {
                    currLiliesNum -= 2;
                }
                else if (currSum < flowersForOneWreath)
                {
                    storedFlowersForLater += (currLiliesNum + currRosesNum);
                    liliesStack.Pop();
                    rosesStack.Pop();
                }
                else if (currSum == flowersForOneWreath)
                {
                    wreathsCount++;
                    liliesStack.Pop();
                    rosesStack.Pop();
                }

            }

            if (storedFlowersForLater >= flowersForOneWreath)
            {
                storedFlowersForLater -= flowersForOneWreath;
                wreathsCount++;
            }
            
            if (wreathsCount >= wreathsNeeded)
            {
                Console.WriteLine($"You made it, you are going to the competition with {wreathsCount} wreaths!");
            }
            else
            {
                var diff = wreathsNeeded - wreathsCount;
                Console.WriteLine($"You didn't make it, you need {diff} wreaths more!");
            }

Aucun commentaire:

Enregistrer un commentaire