我需要在C#中编写一个简单的测试控制台应用程序,用于学校筹款追踪。这是预期的输出可能是什么样的。用户输入提示将被放置在循环内,因此程序继续询问输入,直到为用户的等级输入'999'。如果/当输入999值时,程序应计算贡献的平均值,并将结果显示给最大的收集者祝贺消息。
//创建一个将实现所需私有变量的类。创建类方法以完成所需的功能。在main方法中,实例化一个筹款对象,并使用该对象访问私有变量。这个问题甚至有意义吗?你如何开设课程来完成所有这些任务?
Sample output:
/*School Fund Raiser Tracker Application */
Please enter the grade of the contributor (6, 7 or 8)
(Enter 999 to exit): 6
Please enter the amount of the contribution: 100
Please enter the grade of the contributor (6, 7 or 8)
(Enter 999 to exit): 6
Please enter the amount of the contribution: 200
Please enter the grade of the contributor (6, 7 or 8)
(Enter 999 to exit): 7
Please enter the amount of the contribution: 500
Please enter the grade of the contributor (6, 7 or 8)
(Enter 999 to exit): 7
Please enter the amount of the contribution: 100
Please enter the grade of the contributor (6, 7 or 8)
(Enter 999 to exit): 999
Grade Level # of Contrib Total Amount Average
6 2 300 150
7 2 600 300
我对它的看法:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace FundRaiser
{
public class SchoolFundRaiser
{
//public int grade { get; set; }
//public double contribution { get; set; }
public double average { get; set; }
public int numOfContributions { get; set; }
public void CalculateContribution()
{
int grade= 0; double contribution = 0.0;
double total6 = 0.0, total7 = 0.0, total8 = 0.0, average6 = 0.0, average7 = 0.0, average8 = 0.0;
int count6 = 0, count7 = 0, count8 = 0;
while (grade != 999)
{
Console.WriteLine("Please Enter your grade(6, 7, 8). Enter 999 to quit.");
grade = Convert.ToInt32(Console.ReadLine());
if (grade != 999)
{
Console.WriteLine("Please Enter the Amount you want to contribute. ");
contribution = Convert.ToDouble(Console.ReadLine());
}
else
{
Console.WriteLine("Exiting...");
}
if (grade == 6)
{
total6 = total6 + contribution;
count6 = count6 +1;
}
if (grade == 7)
{
total7 = total7 + contribution;
count7 = count7 + 1;
}
else if (grade == 8)
{
total8 = total8 + contribution;
count8= count8 + 1;
}
}
average6 = total6 / count6;
average7 = total7 / count7;
average8 = total8 / count8;
Console.WriteLine("Grade \t" + "Num of Contributions \t" + "Total Contribution \t" + "Average contribution");
if (grade == 6)
{
average = average6;
numOfContributions = count6;
Console.WriteLine(grade + "\t" + numOfContributions + "\t" + total6 + "\t" + average);
}
if (grade == 7)
{
average = average7;
numOfContributions = count7;
Console.WriteLine(grade + "\t" + numOfContributions + "\t" + total7 + "\t" + average);
}
else
{
average = average8;
numOfContributions = count8;
Console.WriteLine(grade + "\t" + numOfContributions + "\t" + total8 + "\t" + average);
}
}
}
class Program
{
static void Main(string[] args)
{
SchoolFundRaiser testSchoolFundRaiser = new SchoolFundRaiser();
testSchoolFundRaiser.CalculateContribution();
}
}
}
答案 0 :(得分:1)
如果您想让它更优雅,可以使用Dictionary
和Generic List<>
。下面是接受成绩和贡献的班级,然后显示每个成绩的贡献总数及其平均值。
在List中有Count属性用于获取每个成绩的贡献数量,还有Sum()
方法来获得每个成绩的总贡献,然后最终有Average()
方法来获得平均贡献每年级。
contri.Count, contri.Sum(), contri.Average()
在我使用的词典的键值对中也要注意一点
一个List<double>
的值,以便它可以为每个级别提供与用户想要的一样多的贡献,例如:
private Dictionary<string, List<double>> entries = new Dictionary<string, List<double>>();
以下是最终代码:
class SchoolFundRaising
{
private Dictionary<string, List<double>> entries = new Dictionary<string, List<double>>();
public void displayContributionsSummary()
{
Console.WriteLine("Grade \t Num of Contributions \t Total Contribution \t Average contribution");
foreach (string key in entries.Keys)
{
List<double> contri = entries.FirstOrDefault(k => k.Key == key).Value;
Console.WriteLine("Grade {0} \t {1} \t {2} \t {3}", key, contri.Count, contri.Sum(), contri.Average());
}
}
public void getContributionsGrades()
{
int grade = 0;
double contribution;
while (grade != 999)
{
Console.WriteLine("Please Enter your grade(6, 7, 8). Enter 999 to quit.");
grade = Convert.ToInt32(Console.ReadLine());
if (grade >= 6 && grade <= 8)
{
Console.WriteLine("Please Enter the Amount you want to contribute. ");
contribution = Convert.ToDouble(Console.ReadLine());
if (!entries.ContainsKey(grade.ToString()))
{
List<double> dummyList = new List<double>();
dummyList.Add(contribution);
entries.Add(grade.ToString(), dummyList);
}
else
{
entries.First(k => k.Key == grade.ToString()).Value.Add(contribution);
}
}
else
{
if (grade == 999)
{
Console.WriteLine("Done...");
}
else
{
Console.WriteLine("Grade should either be 6,7 or 8");
}
}
}
}
}
然后你可以像这样打电话给你的主人:
SchoolFundRaising FR = new SchoolFundRaising();
FR.getContributionsGrades();
FR.displayContributionsSummary();
答案 1 :(得分:1)
另一种方法是使用贡献作为对象和字典来保持贡献等级。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace FundRaiser
{
class Program
{
public class GradeContribution
{
public GradeContribution()
{
this.Count = 0;
this.Total = 0.0;
this.Average = 0.0;
}
int grade;
public int Grade
{
get { return grade; }
set { grade = value; }
}
int count;
public int Count
{
get { return count; }
set { count = value; }
}
double total;
public double Total
{
get { return total; }
set { total = value; }
}
double average;
public double Average
{
get { return average; }
set { average = value; }
}
}
public class SchoolFundRaiser
{
Dictionary<int, GradeContribution> contributionReciept;
public SchoolFundRaiser()
{
contributionReciept = new Dictionary<int, GradeContribution>();
}
public void CalculateContribution()
{
int grade = 0;
while (grade != 999)
{
Console.WriteLine("Please Enter your grade(6, 7, 8).\n (Enter 999 to quit):");
grade = Convert.ToInt32(Console.ReadLine());
if (grade != 999)
{
GetContribution(grade);
}
else
{
Console.WriteLine("Exiting...");
DisplayContribution();
}
}
}
void GetContribution(int grade)
{
Console.WriteLine("Please Enter the Amount you want to contribute. ");
double contribution = Convert.ToDouble(Console.ReadLine());
if (contributionReciept.ContainsKey(grade))
{
GradeContribution contribute = contributionReciept[grade];
contribute.Count++;
contribute.Total += contribution;
contribute.Average = (contribute.Total / contribute.Count);
}
else
{
GradeContribution contribute = new GradeContribution();
contribute.Grade = grade;
contribute.Count++;
contribute.Total += contribution;
contribute.Average = (contribute.Total / contribute.Count);
contributionReciept.Add(grade,contribute);
}
}
void DisplayContribution()
{
var list = contributionReciept.Keys.ToList();
list.Sort();
Console.WriteLine("Grade \t" + "Num of Contributions \t" + "Total Contribution \t" + "Average contribution");
foreach (var grade in list)
{
GradeContribution gc = contributionReciept[grade];
Console.WriteLine(gc.Grade + "\t" + gc.Count + "\t" + gc.Total + "\t" + gc.Average);
}
}
}
static void Main(string[] args)
{
SchoolFundRaiser testSchoolFundRaiser = new SchoolFundRaiser();
testSchoolFundRaiser.CalculateContribution();
}
}
}