在我正在编写的应用程序中,其中一种方法允许将用户输入的数字转换为字母。
例如,用户将输入成绩(作为双打),程序将决定(当满足条件时)返回与数字相关联的字母。最初,我是这样写的:
public void GetGrade(double scores)
Console.Write("Score of {0} earns: ", score);
if (score >= 95.0)
Console.WriteLine("A+");
else if (score >= 90.0)
Console.WriteLine("A");
else if (score >= 85.0)
Console.WriteLine("B+");
else if (score >= 80.0)
Console.WriteLine("B");
else if (score >= 75.0)
Console.WriteLine("C+");
else if (score >= 70.0)
Console.WriteLine("C");
else if (score >= 65.0)
Console.WriteLine("D+");
else if (score >= 60.0)
Console.WriteLine("D");
else
Console.WriteLine("F");
但在编写时需要考虑到 RETURN。所以,我认为它应该是公共字符串GetGrade(double scores)
而且因为它在一个数组中,我需要:
foreach(double score in scoress)
{
THE CODE I POSTED ABOVE
}
除了我会把所有的console.writeline
s 都改回来。但是,当我这样做时,我收到一个语法错误,告诉我:
不能在此范围内声明名为 score 的局部变量,因为它会给'score' 赋予不同的含义,'score' 已在父范围或当前范围中用于表示其他内容。
所以,我收集到我不能使用score
,因为标题已经包含score
. 我该如何让它按照我想要的方式工作?