这是我的主要方法,当我打电话给Print状态时。我为PrintStatus设置了参数,我收到了关于没有使用资格的错误,我无法理解。我很擅长传递论据,我们刚刚在课堂上讨论过。
"错误CS0165使用未分配的本地变量'资格' Program10 I:\ Program10 \ Program10 \ Program10.cs 150 Active "
static void Main()
{
int id, age, exp;
double avgAge, avgExp;
char type;
string eligibility;
OpenFiles();
PrintReportHeadings();
while ((lineIn = fileIn.ReadLine()) != null)
{
ParseLineIn(out id, out type, out age, out exp);
PrintStatus(type, age, exp, eligibility);
PrintDetailLine(id, type, age, exp);
}
CloseFiles();
}
不确定如何解决这个问题。
static void PrintStatus(char type, int age, int exp, string eligibility)
{
switch (type)
{
case 'm':
case 'M':
if (age < 55 && exp < 20)
eligibility = ("lack of experience age");
else if (age >= 55 && exp >= 20)
eligibility = ("can retire");
else if (age >= 55 && exp < 20)
eligibility = ("lack of experience");
else if (age < 55 && exp >= 20)
eligibility = ("underage");
else
eligibility = ("Your entry is invalid");
break;
case 'w':
case 'W':
if (age < 63 && exp < 25)
eligibility = ("lack of exp age");
else if (age >= 63 && exp >= 25)
eligibility = ("can retire");
else if (age >= 63 && exp < 25)
eligibility = ("lack of exp");
else if (age < 63 && exp >= 25)
eligibility = ("lack age");
else
eligibility = ("Your entry is invalid");
break;
case 's':
case 'S':
if (age < 60 && exp < 24)
eligibility = ("lack of exp age");
else if (age >= 60 && exp >= 24)
eligibility = ("can retire");
else if (age >= 60 && exp < 24)
eligibility = ("lack of exp");
else if (age < 60 && exp >= 24)
eligibility = ("underage");
else
eligibility = ("Your entry is invalid");
break;
}
}
答案 0 :(得分:1)
如果要使用PrintStatus代码初始化字符串 eligibility ,那么最简单的方法是返回字符串并在返回时将其分配给 eligibility PrintStatus
static string PrintStatus(char type, int age, int exp)
{
string result = "";
switch (type)
{
case 'm':
case 'M':
if (age < 55 && exp < 20)
result = ("lack of experience age");
else if (age >= 55 && exp >= 20)
result = ("can retire");
else if (age >= 55 && exp < 20)
result = ("lack of experience");
else if (age < 55 && exp >= 20)
result = ("underage");
else
result = ("Your entry is invalid");
break;
// etc other case
}
return result:
}
此时你以这种方式调用PrintStatus
....
eligibility = PrintStatus(type, age, exp);
....
要了解实际代码无法更改资格字符串的原因,您应该搜索按值传递参数和通过引用传递的概念。一个很好的解释来自这篇着名的文章
答案 1 :(得分:0)
错误消息已经说明了。在使用之前,您的本地变量资格未分配值。
string eligibility = "whatever";
将删除错误消息。
此外,您的功能应定义如下:
static void PrintStatus(char type, int age, int exp, out string eligibility);
答案 2 :(得分:0)
因为变量是本地的,所以需要为它们分配一个默认值:
updatedTime
对所有局部变量执行相同的操作。
答案 3 :(得分:0)
看起来您想要获得您的资格值。假设是这种情况,我重新编写了PrintStatus方法并将其重命名为GetPrintStatus。
static string GetPrintStatus(char type, int age, int exp)
{
switch (char.ToLower(type))
{
case 'm':
if (age < 55 && exp < 20)
return ("lack of experience age");
if (age >= 55 && exp >= 20)
return ("can retire");
if (age >= 55 && exp < 20)
return ("lack of experience");
if (age < 55 && exp >= 20)
return ("underage");
return ("Your entry is invalid");
case 'w':
if (age < 63 && exp < 25)
return ("lack of exp age");
if (age >= 63 && exp >= 25)
return ("can retire");
if (age >= 63 && exp < 25)
return ("lack of exp");
if (age < 63 && exp >= 25)
return ("lack age");
return ("Your entry is invalid");
case 's':
if (age < 60 && exp < 24)
return ("lack of exp age");
if (age >= 60 && exp >= 24)
return ("can retire");
if (age >= 60 && exp < 24)
return ("lack of exp");
if (age < 60 && exp >= 24)
return ("underage");
return ("Your entry is invalid");
}
return string.Empty;
}
要使用它,您必须在main方法中包含以下行。替换它;
PrintStatus(type, age, exp, eligibility);
白衣;
eligibility = GetPrintStatus(type, age, exp);
type,age和exp必须给出这样的值;
age = 35;
必须在调用GetPrintStatus方法之前完成此操作。
因为你正在学习我会解释一些变化; GetPrintStatus旁边是单词&#34; string&#34;,这意味着该函数将返回一个字符串(以前称之为资格)和char.ToLower(type)使你传递的字符小写,所以它不再无论是M还是M。这可以进一步简化,但它看起来不像你给的代码。
祝你好运。