C#如何计算输入的数量以找到开关回路中的平均值?

C#如何计算输入的数量以找到开关回路中的平均值?,c#,loops,count,switch-statement,average,C#,Loops,Count,Switch Statement,Average,这是我的循环,它询问组号,然后询问捐赠。我想知道如何计算捐款的数量,以找出每个群体的平均数 using System; public class TotalPurchase { public static void Main() { double total4 = 0; double total5 = 0; double total6 = 0; int myint = -1; while (myint != 0) {

这是我的循环,它询问组号,然后询问捐赠。我想知道如何计算捐款的数量,以找出每个群体的平均数

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

    }
}

任何帮助都将不胜感激。

只需在案例陈述中声明每组的计数以及总数和增量:

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;
或者,您可以使用
列表
,也可以计算您的平均值:

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


不确定我是否完全理解您的问题,但您可以为每组添加一个简单的计数器:

int donations4 = 0;
int donations5 = 0;
int donations6 = 0;
然后在每个开关箱中增加该计数器,例如:

switch(myInt)
{
   case 4:
     ...
     donations4++;
     break;
   case 5:
     ...
     donations5++;
     break;
   case 6:
     ...
     donations6++;
     break;
}
然后,当你完成时,简单地做数学运算,找出平均值

虽然这可能是最简单的方法,但更好的方法是将每个群体视为自己的对象,让对象在内部跟踪捐赠金额以及金额和平均数


--Dan

用另一个变量记录自己的计数<代码>计数4、计数5等

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

    }
}

如您所见,有3个额外变量(每组一个)可用于计算提供的输入数量。使用该方法,您可以将每组的总数分别除以每组的输入数。

有关家庭作业的加分:

1) 清理您的组号输入-即检查用户输入的组号是否有效

2) 不要调用变量myInt。称它为groupNum,或者描述函数的东西,而不是变量的实现

3) 使用数组计算捐赠总额和计数 即

然后在循环中(甚至不需要开关):


我会将您的double改为List,并在最后使用列表中的Sum()和Average()方法。更改后,您的代码将如下所示

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

    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
公共类总购买
{
公共静态void Main()
{
List total4=新列表();
List total5=新列表();
List total6=新列表();
int-myint=-1;
while(myint!=0)
{
字符串组;
Console.WriteLine(“请输入组号(4、5或6)”;
Console.WriteLine((0表示退出):);
group=Console.ReadLine();
myint=Int32.Parse(组);
开关(myint)
{
案例0:
打破
案例4:
双重捐赠4;
字符串输入string4;
Console.WriteLine(“请输入供款金额:”);
inputString4=Console.ReadLine();
donation4=Convert.ToDouble(inputString4);
总计4.添加(捐赠4);
打破
案例5:
双重捐赠5;
字符串输入String5;
Console.WriteLine(“请输入供款金额:”);
inputString5=Console.ReadLine();
donation5=Convert.ToDouble(inputString5);
总计5.添加(捐赠5);
打破
案例6:
双重捐赠6;
字符串输入字符串6;
Console.WriteLine(“请输入供款金额:”);
inputString6=Console.ReadLine();
donation6=Convert.ToDouble(inputString6);
总计6.添加(捐赠6);
打破
违约:
Console.WriteLine(“不正确的等级编号”,myint);
打破
}
}
如果(总计4.Count>0)
WriteLine(“四级总计为{0};平均值为{1}”)、total4.Sum().ToString(“C”)、total4.Average().ToString(“C”);
如果(总数5.Count>0)
WriteLine(“5级总计为{0};平均值为{1}”)、total5.Sum().ToString(“C”)、total5.Average().ToString(“C”);
如果(总计6.Count>0)
WriteLine(“6级总计为{0};平均值为{1}”)、total6.Sum().ToString(“C”)、total6.Average().ToString(“C”);
}
}

开关
不是循环,你的标题毫无意义。您是否正在尝试计算每个类别中的项目数?可能重复投票关闭,因为它看起来太像上一个。乔尔,你应该熟悉字典
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"));

    }
}
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
++donationCount[groupNum];
donationTotal[groupNum] += donationAmount;    // did you notice that you moved the reading of donationAmount out of the switch?
    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"));

    }
}