Display running total

可紊 提交于 2019-12-14 00:03:01

问题


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);
}

回答1:


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

Read Here



来源:https://stackoverflow.com/questions/46826299/display-running-total

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!