本文介绍了重用适用于每个类别的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我需要编写一个计算学生学期成绩的程序。我只能使用一个功能,学生要输入每个类别中有多少年级......(例如考试数量是多少?学生回答5,2,90他们选择的任何等级然后他们进入每个考试,作业等等的等级。完成后,程序应计算平均值并显示给用户,这是我遇到最大困难的地方。我不知道如何显示每个个别类别,然后让程序继续下一个类别。最后我必须取所有这些平均值并计算最终总成绩,我不知道该怎么做,因为所有平均值都是在一个函数下计算的。到目前为止,这是我的整个代码。非常感谢任何输入..我不是要求你做我的工作,而是至少得到一些指导..抱歉它看起来有多乱,但我更新了论坛谢谢I need to write a program that calculates the grade of a students semester. I can only use one function and the student is to enter how many grades there are in each category... (ex. "How many number of exams?" students response "5,2,90" whatever they choose and then they enter the grade for each number of exams, homework etc. After that is done the program should calculate the average and display it to the user and that is where I''m having the most difficulty. I dont know how to display the average for each individual category and then have the program continue to the next category. In the end I must take all of these averages and calculate a Final total grade and I dont know how to do that since all of the averages are to be calculated under one function.. this is my entire code so far..any input would be greatly appreciated.. I''m not asking you to do my work but for some guidance at least.. Sorry for how messy it appears but I''m newer to the forum Thank younamespace HW2{ class IntroCS { public static void Main (string[] args) { WeightTotal (); double exAvg = FindAvg("exam"); double hwAvg = FindAvg("homework"); double labAvg = FindAvg("lab"); double parAvg = FindAvg("participation"); double projAvg = FindAvg("project"); } public static void WeightTotal () { int exam_weight, hw_weight, proj_weight, part_weight, lab_weight; do { Console.WriteLine ("Please enter the weight for each section "); exam_weight = UIF.PromptInt ("Enter your exam weight: "); hw_weight = UIF.PromptInt ("Enter your homework weight: "); proj_weight = UIF.PromptInt ("Enter your project weight: "); part_weight = UIF.PromptInt ("Enter your paricipation weight: "); lab_weight = UIF.PromptInt ("Enter your lab weight: "); if (exam_weight + hw_weight + proj_weight + part_weight + lab_weight != 100) { Console.WriteLine (" Total weights did not equal 100! Try again. "); } } while (exam_weight + hw_weight + proj_weight + part_weight + lab_weight != 100); } public static double FindAvg (string name) { double totalAvg = 0; double Num = UIF.PromptDouble ("Enter the number of " + name + "s: "); for (int i = 1; i <= Num; i++) { double grade = UIF.PromptDouble ("Enter the grade for "+name+" #" + i + ":"); } totalAvg = totalAvg + grade; double Avg = totalAvg / Num; Console.WriteLine ("Calculated average " + name + " grade: " + Avg); } }} 索引已更改为了更好的可读性 - Nelek [/ edit]推荐答案int exam_weight, hw_weight, proj_weight, part_weight, lab_weight; 一旦 WeightTotal()完成,就会消失在烟雾中。 你需要的是制作那些值Instance Variables,这意味着它们可用于这两个函数。只需移动上面写的代码行:Disappear in a puff of smoke as soon as WeightTotal() completes.What you need is to make those values Instance Variables, which means they will be available to both functions. Simply move the line of code written above in between:class IntroCS{ 和andpublic static void Main (string[] args) 您现在可以在第二个函数中引用这些值。You can now reference those values in the second function. 这篇关于重用适用于每个类别的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!