简体   繁体   中英

Calculating average of all user input numbers using while loops in C#

I'm trying to work through a problem presented in one of my classes. The prompt is telling us to get the user to enter any numbers (can be positive, negative, or 0), while ignoring non-numeric inputs. Then we need to compute and display the average of all the numbers entered by the user. If the user doesn't give any numbers, I need to output "you didn't enter any numbers".

My main issue is that I'm not able to store and add the numbers given by the user properly. I'm fairly certain that everything before and after the while statement is sound. So, I know the issue must lie within while (enter!="Yes"||enter!="yes"||enter!="Y"||enter!="y") , but I'm not exactly sure what the issue is. Since I have variables for my average, the sum of the user given numbers, and a counter to keep track of loop iterations, I'm pretty sure my troubles are coming from my code not being in the correct order.

Console.WriteLine("Please enter any numbers, then type Yes to continue.");
        string enter = Console.ReadLine();
        string msg = "";
        decimal average;
        int counter = 0;
        decimal sum = 0;
        bool res = decimal.TryParse(enter, out average);
        while (enter!="Yes"||enter!="yes"||enter!="Y"||enter!="y")
        {
            sum = decimal.Parse(enter);
            Console.WriteLine("Please enter any numbers, then type Yes to continue");
            enter = Console.ReadLine();
            sum += counter;
            counter++;
        }
        average = sum / counter;
        msg = (res) ? $"The sum of your numbers is {average}" : "You didn't enter any numbers";
        Console.WriteLine(msg);

try this one

static void Main()
{
    int counter = 0;
    decimal sum = 0;
    bool exit=false;    
    do 
    {
        Console.WriteLine("Please enter any number or type \"done\" to exit");
        var enter = Console.ReadLine();
        if (enter.Trim().ToLower() != "done")
        {
            var ok = decimal.TryParse(enter, out var num);
            if(!ok) continue;
            sum += num;
            counter++;
        } else exit=true;
        
    } while (!exit);
    
     var average = counter > 0 ? sum / counter:0;
     var msg = average>0?  $"The average  of your numbers is {average}" : "You didn't enter any numbers";
    Console.WriteLine(msg);
}

Here's an alternative for you to play with. Just to give you some more ideas.

string enter = "";
string[] stop = new [] { "yes", "y" };
List<int> numbers = new List<int>();
while (!stop.Contains(enter.ToLowerInvariant()))
{
    Console.WriteLine("Please enter any numbers, then type Yes to continue.");
    enter = Console.ReadLine();
    if (int.TryParse(enter, out int number))
    {
        numbers.Add(number);
    }
}
if (numbers.Any())
{
    Console.WriteLine($"The average of your numbers is {numbers.Average()}");
}
else
{
    Console.WriteLine("You didn't enter any numbers");
}

Try this......

    string enter = "";
    string msg = "";
    decimal average;
    int counter = 0;
    decimal sum = 0;
    decimal input;
    
    while (enter!="Yes"&&enter!="yes"&&enter!="Y"&&enter!="y")
    {
            Console.WriteLine("Please enter any numbers, then type Yes to continue");
            enter = Console.ReadLine();
            bool res = decimal.TryParse(enter, out input);
            if (res) { 
                sum += input;
                counter++;
            }
    }
    if (counter != 0)
    {
        average = sum / counter;
        msg = $"The average of your numbers is {average}";
    }
    else {
        msg = "You didn't enter any numbers";
    }

    Console.WriteLine(msg);
    System.Threading.Thread.Sleep(5000);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM