简体   繁体   English

C#如何计算在开关循环中找到平均值的输入数量?

[英]C# How do you count the number of inputs to find the average in a switch loop?

Here is my loop that asks for the group number then the donation. 这是我的循环,询问组号然后捐赠。 I'm wondering how to count the number of donations to find the average for each group. 我想知道如何计算捐款数量以找出每组的平均值。

using System;
public class TotalPurchase
{
    public static void Main()
    {

    double total4 = 0;
    double total5 = 0;
    double total6 = 0;
    int myint = -1;

    while (myint != 0)
    {
        string group;

        Console.WriteLine("Please enter group number (4, 5, or 6)");
        Console.WriteLine("(0 to quit): ");
        group = Console.ReadLine();
        myint = Int32.Parse(group);

        switch (myint)
        {
            case 0:
                break;
            case 4:
                double donation4;
                string inputString4;
                Console.WriteLine("Please enter the amount of the contribution: ");
                inputString4 = Console.ReadLine();
                donation4 = Convert.ToDouble(inputString4);
                total4 += donation4;
                break;
            case 5:
                double donation5;
                string inputString5;
                Console.WriteLine("Please enter the amount of the contribution: ");
                inputString5 = Console.ReadLine();
                donation5 = Convert.ToDouble(inputString5);
                total5 += donation5;
                break;
            case 6:
                double donation6;
                string inputString6;
                Console.WriteLine("Please enter the amount of the contribution: ");
                inputString6 = Console.ReadLine();
                donation6 = Convert.ToDouble(inputString6);
                total6 += donation6;
                break;
            default:
                Console.WriteLine("Incorrect grade number.", myint);
                break;
        }
    }

    Console.WriteLine("Grade 4 total is {0}", total4.ToString("C"));
    Console.WriteLine("Grade 5 total is {0}", total5.ToString("C"));
    Console.WriteLine("Grade 6 total is {0}", total6.ToString("C"));

    }
}

Any help would be appreciated. 任何帮助,将不胜感激。

Not sure if I fully understand your question -- but you could just add a simple counter for each group: 不确定我是否完全理解你的问题 - 但你可以为每个组添加一个简单的计数器:

int donations4 = 0;
int donations5 = 0;
int donations6 = 0;

And then increment that counter in each of your switch cases, ex: 然后在每个交换机案例中增加该计数器,例如:

switch(myInt)
{
   case 4:
     ...
     donations4++;
     break;
   case 5:
     ...
     donations5++;
     break;
   case 6:
     ...
     donations6++;
     break;
}

Then when you're done - simply do the math to find the average. 然后当你完成时 - 只需要数学来找到平均值。

Although this is probably the simplest way, a better way would be to treat each group as its own object, and have the object internally track the # of donations, as well as the sum and average. 虽然这可能是最简单的方法,但更好的方法是将每个组视为自己的对象,并让对象在内部跟踪捐赠数量,以及总和和平均值。

-- Dan - 丹

Just declare count for each group as well as total and increment in the case statement: 只需声明每个组的计数以及case语句中的总计和增量:

case 4:
            double donation4;
            string inputString4;
            Console.WriteLine("Please enter the amount of the contribution: ");
            inputString4 = Console.ReadLine();
            donation4 = Convert.ToDouble(inputString4);
            total4 += donation4;
            count4++; // HERE!!!!
            break;

Alternatively, you can use List<int> which will calculate your average as well: 或者,您可以使用List<int>来计算您的平均值:

 List<int> list4 = new List<int>();

and

case 4:
            double donation4;
            string inputString4;
            Console.WriteLine("Please enter the amount of the contribution: ");
            inputString4 = Console.ReadLine();
            donation4 = Convert.ToDouble(inputString4);
            list4.Add(donation4);
            break;

and

 Console.WriteLine(list4.Average());

Just keep track of the count yourself with another variable. 只需用另一个变量跟踪计数。 count4 , count5 , etc. count4count5等。

using System;

public class TotalPurchase
{
    public static void Main()
    {
       double total4 = 0;
       double total5 = 0;
       double total6 = 0;

       int numberOfInputForTotal4 = 0;
       int numberOfInputForTotal5 = 0;
       int numberOfInputForTotal6 = 0;

       int myint = -1;

        while (myint != 0)
        {
            string group;

            Console.WriteLine("Please enter group number (4, 5, or 6)");
            Console.WriteLine("(0 to quit): ");
            group = Console.ReadLine();
            myint = Int32.Parse(group);

            switch (myint)
            {
                case 0:
                    break;
                case 4:
                    double donation4;
                    string inputString4;
                    Console.WriteLine("Please enter the amount of the contribution: ");
                    inputString4 = Console.ReadLine();
                    donation4 = Convert.ToDouble(inputString4);
                    total4 += donation4;
                    numberOfInputForTotal4++;
                    break;
                case 5:
                    double donation5;
                    string inputString5;
                    Console.WriteLine("Please enter the amount of the contribution: ");
                    inputString5 = Console.ReadLine();
                    donation5 = Convert.ToDouble(inputString5);
                    total5 += donation5;
                    numberOfInputForTotal5++;
                    break;
                case 6:
                    double donation6;
                    string inputString6;
                    Console.WriteLine("Please enter the amount of the contribution: ");
                    inputString6 = Console.ReadLine();
                    donation6 = Convert.ToDouble(inputString6);
                    total6 += donation6;
                    numberOfInputForTotal6++;
                    break;
                default:
                    Console.WriteLine("Incorrect grade number.", myint);
                    break;
            }
        }

        Console.WriteLine("Grade 4 total is {0}", total4.ToString("C"));
        Console.WriteLine("Grade 5 total is {0}", total5.ToString("C"));
        Console.WriteLine("Grade 6 total is {0}", total6.ToString("C"));

        Console.WriteLine("Grade 4 average is {0}", (total4 / numberOfInputForTotal4).ToString("C"));
        Console.WriteLine("Grade 5 average is {0}", (total5 / numberOfInputForTotal5).ToString("C"));
        Console.WriteLine("Grade 6 average is {0}", (total6 / numberOfInputForTotal6).ToString("C"));

    }
}

As you can see, there are 3 extra variables (one for each group) that can be used to figure out the number of inputs provided. 如您所见,有3个额外变量(每组一个)可用于计算所提供的输入数量。 Using that you can divide the total for each group by the number of input in each group separately. 使用它可以将每组的总数除以每组中的输入数量。

For bonus points in your homework assignment: 对于家庭作业的奖励积分:

1) Sanitize your group number input - ie check to see if the user typed in a valid number. 1)清理您的组号输入 - 即检查用户是否输入了有效号码。

2) Don't call the variable myInt. 2)不要调用变量myInt。 Call it groupNum, or something that describes the function, not the implementation of the variable. 称之为groupNum,或描述函数的东西,而不是变量的实现。

3) Use an array for donation totals and counts ie, 3)使用数组进行捐赠总计和计数,即

int[] donationCount= new int[MAX_GROUP+1];    // figure out yourself why the +1
int[] donationTotal= new int[MAX_GROUP+1];
// initialize donationCount and donationTotal here

then in your loop (don't even need switch): 然后在你的循环中(甚至不需要切换):

++donationCount[groupNum];
donationTotal[groupNum] += donationAmount;    // did you notice that you moved the reading of donationAmount out of the switch?

I would go with changing your doubles to List and using the Sum() and Average() methods on your Lists at the end. 我会将你的双打更改为List并在结尾处使用列表中的Sum()和Average()方法。 Your code would look like this after this change. 在此更改后,您的代码将如下所示。

    using System;
using System.Collections.Generic;
using System.Linq;
public class TotalPurchase
{
    public static void Main()
    {

        List<double> total4 = new List<double>();
        List<double> total5 = new List<double>();
        List<double> total6 = new List<double>();
        int myint = -1;

        while (myint != 0)
        {
            string group;

            Console.WriteLine("Please enter group number (4, 5, or 6)");
            Console.WriteLine("(0 to quit): ");
            group = Console.ReadLine();
            myint = Int32.Parse(group);

            switch (myint)
            {
                case 0:
                    break;
                case 4:
                    double donation4;
                    string inputString4;
                    Console.WriteLine("Please enter the amount of the contribution: ");
                    inputString4 = Console.ReadLine();
                    donation4 = Convert.ToDouble(inputString4);
                    total4.Add(donation4);
                    break;
                case 5:
                    double donation5;
                    string inputString5;
                    Console.WriteLine("Please enter the amount of the contribution: ");
                    inputString5 = Console.ReadLine();
                    donation5 = Convert.ToDouble(inputString5);
                    total5.Add(donation5);
                    break;
                case 6:
                    double donation6;
                    string inputString6;
                    Console.WriteLine("Please enter the amount of the contribution: ");
                    inputString6 = Console.ReadLine();
                    donation6 = Convert.ToDouble(inputString6);
                    total6.Add(donation6);
                    break;
                default:
                    Console.WriteLine("Incorrect grade number.", myint);
                    break;
            }
        }

        if(total4.Count > 0)
            Console.WriteLine("Grade 4 total is {0}; Average {1}", total4.Sum().ToString("C"), total4.Average().ToString("C"));
        if(total5.Count >0)
            Console.WriteLine("Grade 5 total is {0}; Average {1}", total5.Sum().ToString("C"), total5.Average().ToString("C"));
        if (total6.Count > 0)
            Console.WriteLine("Grade 6 total is {0}; Average {1}", total6.Sum().ToString("C"), total6.Average().ToString("C"));

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在C#中循环计数数字? - How to make number Count in loop in c#? 您如何计算 C# 中给定数据集的移动平均值? - How do you compute for the moving average of a given dataset in C#? 在 C# 中,如何使用反射计算表达式主体 output function 中包含的属性数量? - In C#, how do you count the number of properties contained within an expression body output function using reflection? 如何使用C#计算段落中某个单词的数量 - How do you count the number of a certain word in a paragraph using C# C#:使用 Do While 循环计算字符串中的位数 - C#: Count Number of Digits in a String Using Do While Loop 如何在 C# 中运行可变数量的并发参数化无限循环类型的线程? - How do you run a variable number of concurrent parametrizable infinite loop type of threads in C#? 如何循环 C# 中的 switch 语句 - How do I loop round a switch statement in C# 如何在 C# 中循环切换函数 - How do I loop a switch function in C# 你如何计算 c# 中单个字符串中的数字 - how do you count numbers in a single string in c# 在 Z0F4137ED1502B5045D6083AA258B5C42DC28CEECA2576AABEEC2ECE34Z 中输入行数和列数后,你怎么做 output 一个嵌套循环到 label? - How do you do output a nested loop of symbols to a label after inputting the number of rows and columns in windows form c#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM