我想調用類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