【发布时间】:2022-06-12 02:30:32
【问题描述】:
这个函数接受输入并告诉用户输入的是数字还是非数字。
static string isnum()
{
Console.WriteLine("Write a number please");
string a = Console.ReadLine();
string nums = "123456789";
int cnt = 0;
for (int i = 0; i < a.Length; i++)
{
for (int j = 0; j < nums.Length; j++)
{
if (a[i] == nums[j])
{
cnt++;
break;
}
}
}
if (cnt == a.Length)
{
Console.WriteLine(a + " is a number");
return a;
}
else
{
Console.WriteLine(a + " is not a number");
return "";
}
}
isnum();
如果输入不是数字,我希望此函数重复自己,直到输入为数字,然后停止。 这个功能现在有效,但她只工作一次。 当我尝试向函数添加一个 while 块以使她一次又一次地运行直到输入为数字时,我收到“并非所有代码路径都返回值”错误。
是不是因为“return”语句结束了一个函数,因此阻止了她再次运行? 我该如何解决?
非常感谢!
【问题讨论】:
-
请告诉我们无效的代码。
-
我还敢猜测
0在数字上是合法的。
标签: c# function while-loop return