【发布时间】:2015-04-02 03:29:57
【问题描述】:
我只是很困惑,需要一些帮助。我真的不知道在 numCheck 方法中返回 num 是怎么回事。我的老师说它现在应该看起来如何,所以返回了什么,我该如何使用它?我正在尝试制作一个程序来询问数学问题的答案(例如 2 + 2),如果用户输入为 4,它会结束,但它会再次询问其他任何问题。这应该需要5分钟,我只是不明白他的意思。谢谢!
namespace readNumber
{
class Program
{
static void Main(string[] args)
{
//should be a loop around this method call
//for(......)
numCheck();
Console.ReadLine();
}
static int numCheck()
{
bool itWorked;
int num;
do
{
Console.Write("Enter a Number: ");
string numSt = Console.ReadLine();
Console.WriteLine(numSt);
itWorked = int.TryParse(numSt, out num);
if (itWorked)
{
Console.WriteLine("Integer assigned: " + num);
}
else
{
Console.WriteLine("It Failed");
}
}
while (!itWorked);
return num;
}
}
}
每当我尝试在主程序中使用 num 时,我得到最多的错误是“名称“num”在当前上下文中不存在”
【问题讨论】:
-
为我工作,没有错误。还有其他代码吗?
-
我没有发现任何问题。错误应该告诉你哪一行..
标签: c# loops text input return