Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
343 views
in Technique[技术] by (71.8m points)

c# - Display running total

Hi I was wondering if someone could explain how to to make this method diplay a running count and average, and not just display it once the user has finished entering its data?

public void InScreen()
{
    int count = 0;
    double total = 0.0;
    double average = 0.0;
    double number;

    Console.WriteLine("Enter the set of scores (enter 0 to indicate end of set)");

    number = double.Parse(Console.ReadLine());

    while(number != 0)
    {
        total += number;
        count++;
        number = double.Parse(Console.ReadLine());
    }

    if (count != 0)
       average = total / count;

    Console.Beep(20000, 2000);
    Console.WriteLine("The user has entered {0} scores.", count);
    Console.WriteLine("The sum of scores entered = {0}", total);
    Console.WriteLine("The average of scores entered = {0}", average);
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Simply try this

   static void Main(string[] args)
    {
        try
        {
            StringBuilder runningtotal = new StringBuilder();

            int count = 0;
            double total = 0.0;
            double average = 0.0;
            double number;

            Console.WriteLine("Enter the set of scores (enter 0 to indicate end of set)");

            number = double.Parse(Console.ReadLine());
            runningtotal.Append(number.ToString());
            while (number != 0)
            {

                total += number;
                count++;
                number = double.Parse(Console.ReadLine());
                if (number!=0)
                {
                    runningtotal.Append("+" + number.ToString());
                }

            }
            if (count != 0)
                average = total / count;
            Console.Beep(20000, 2000);
            Console.WriteLine("The user has entered {0} scores.", count);
            Console.WriteLine("The sum of scores entered = {0}", total);
            Console.WriteLine("The average of scores entered = {0}", average);
            Console.WriteLine(runningtotal);
            string[] inputs = runningtotal.ToString().Split('+');
            Console.WriteLine("Running total");
            int temp=0;
            for (int i = 0; i <inputs.Length; i++)
            {
                if (temp==0)
                {
                    Console.WriteLine("{0} = {1}",inputs[i],inputs[i]);
                    temp = Convert.ToInt32(inputs[i]);
                }
                else
                {
                    Console.WriteLine("{0} = {1}", inputs[i], Convert.ToInt32(inputs[i]) + temp);
                    temp = Convert.ToInt32(inputs[i]) + temp;
                }



            }

            Console.ReadLine();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            Console.ReadLine();
        }

        Console.ReadLine();
    }

Output Screenshot

enter image description here

Read Here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...