这是我的循环,询问组号然后捐赠。我想知道如何计算捐款数量以找出每组的平均值。
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"));
}
}
任何帮助都将不胜感激。
答案 0 :(得分:2)
不确定我是否完全理解您的问题 - 但您可以为每个组添加一个简单的计数器:
int donations4 = 0;
int donations5 = 0;
int donations6 = 0;
然后在每个交换机案例中增加该计数器,例如:
switch(myInt)
{
case 4:
...
donations4++;
break;
case 5:
...
donations5++;
break;
case 6:
...
donations6++;
break;
}
然后当你完成时 - 只需做数学计算即可找到平均值。
虽然这可能是最简单的方法,但更好的方法是将每个组视为自己的对象,让对象在内部跟踪捐赠数量,以及总和和平均值。
- 丹
答案 1 :(得分:0)
只需在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;
或者,您也可以使用List<int>
来计算您的平均值:
List<int> list4 = new List<int>();
和
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;
和
Console.WriteLine(list4.Average());
答案 2 :(得分:0)
使用另一个变量自己跟踪计数。 count4
,count5
等
答案 3 :(得分:0)
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个额外变量(每组一个)可用于计算所提供的输入数量。使用它可以将每组的总数除以每组中的输入数。
答案 4 :(得分:0)
您的家庭作业奖励积分:
1)清理您的组号输入 - 即检查用户是否输入了有效号码。
2)不要调用变量myInt。称之为groupNum,或描述函数的东西,而不是变量的实现。
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
然后在你的循环中(甚至不需要切换):
++donationCount[groupNum];
donationTotal[groupNum] += donationAmount; // did you notice that you moved the reading of donationAmount out of the switch?
答案 5 :(得分:0)
我会将你的双打更改为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"));
}
}