'Why does my 'averageScore' come out as 0?

why does my code not calculate an average score when entering "-1" into the console? It comes up at 0. It's a part of a loop exercise, so I'm sure there are faster ways to code this. I want to fix it within my current C# comprehension.

Here's the task

using System;

namespace Challenge_Loops1
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string individualScore = "0";
            int scoreCount = 0;
            int totalScore = 0;
            int individualScoreIntoInt = 0;

            while (individualScore != "-1")
            {
                Console.WriteLine($"Last number was {individualScoreIntoInt}");
                Console.WriteLine("Please enter the next score");
                Console.WriteLine($"Current amount of entries: {scoreCount}");
                Console.WriteLine("Enter '-1' when you're ready to calculaate the average");

                individualScore = Console.ReadLine();

                if (individualScore.Equals("-1"))
                {
                    Console.WriteLine("--------------------------------------------");
                    double averageScore = (double)totalScore / (double)scoreCount;
                    Console.WriteLine($"The average total score is {averageScore}");
                    
                    if(int.TryParse(individualScore, out individualScoreIntoInt) && individualScoreIntoInt > 0 && individualScoreIntoInt < 21)
                    {
                        totalScore += individualScoreIntoInt;
                        //longer way: totalScore = individualScoreIntoInt + totalScore;
                    }
                    else if(individualScoreIntoInt < 0 || individualScoreIntoInt < 20)
                    {
                        Console.WriteLine("Enter a score > 0 and < 21");
                        continue;
                    }
                    else
                    {
                        Console.WriteLine("Please only enter numbers");
                    }
                
                }

                scoreCount++; // adding the individualscore entered to the count. writing it here so that it's only
                //added to the count if it meets the requirements

            }
        }
    }
}


Solution 1:[1]

Order of operations was incorrect: 1st validate if it's -1 or not, 2nd parse value and if it's possible perform below operations, if not drop error. This was logic issue, rather than code itself. You had added iteration despite exceptions, you didn't include possibility of 21 etc.

namespace Challenge_Loops1
    {
        internal class Program
        {
            static void Main(string[] args)
            {
                string individualScore = "0";
                int scoreCount = 0;
                int totalScore = 0;
                int individualScoreIntoInt = 0;
    
                while (individualScore != "-1")
                {
                    Console.WriteLine($"Last number was {individualScoreIntoInt}");
                    Console.WriteLine("Please enter the next score");
                    Console.WriteLine($"Current amount of entries: {scoreCount}");
                    Console.WriteLine("Enter '-1' when you're ready to calculaate the average");
    
                    individualScore = Console.ReadLine();
    
                    if (individualScore.Equals("-1"))
                    {
                        Console.WriteLine("--------------------------------------------");
                        double averageScore = (double)totalScore / (double)scoreCount;
                        Console.WriteLine($"The average total score is {averageScore}");
                    }
                    else if (int.TryParse(individualScore, out individualScoreIntoInt))
                    {
                        if(individualScoreIntoInt > 0 && individualScoreIntoInt <= 21)
                        {
                            totalScore += individualScoreIntoInt;
                            scoreCount++;
                        }
//as mentioned in comment else here would also work, it's unnecessary to add any other validation.
                        else if (individualScoreIntoInt < 0 || individualScoreIntoInt > 21)
                        { 
                            Console.WriteLine("Enter a score > 0 and < 21");
                        }
                    }
                    else
                    {
                        Console.WriteLine("Please only enter numbers");
                    }
                }
            }
        }
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1