我想调用类StudentApp的Promotion值,所以我可以在GetTotalScore中总结它。 这里简单的代码示例.. 已更新代码。从另一个类调用一个值
class Tester
{
static void Main(string[] args)
{
Console.WriteLine("Total score (after 10 marks promotion): " + GetTotalScore(app.Students));
}
static double GetTotalScore(List<Student> list)
{
if (list.Count == 0)
{
return 0;
}
else
{
List<Student> subList = list.GetRange(1, list.Count - 1);
double subTotal = GetTotalScore(subList);
double total = .....[0] + subTotal;
return total;
}
}
}
class StudentApp
{
public void PromoteScore(List<Student> list)
{
double Promotion = 0;
foreach (Student s in Students)
{
if (s.Score + 10 > 100)
{
Promotion = 100;
}
else
{
Promotion = s.Score + 10;
}
}
}
}
任何帮助被赞赏!
你不能,该变量对于'PromoteScore'方法是局部的。您需要从方法中返回它,或者以其他方式使其可访问(例如:一个'公共'成员变量或属性) – UnholySheep