对于我正在尝试编写的程序,我需要同时使用Switch案例和While循环。我对Switch示例没有任何问题,但我不确定如何使用While循环获取类似的解决方案。嵌套的while循环其中“不是所有代码路径都返回一个值”
我想用一个嵌套的while循环来评估用户输入的值。 在这里面我想允许用户“选择”任意数量之间并包括3至10(假设使用系统等是正确的)
static void Main()
{
int userInput; //user enters int with ReadLine
int defaultInt = 3; //user keys anything other than 3 through 10
Console.WriteLine("Enter an Integer");
userInput = int.Parse(Console.ReadLine());
InputCheck();
Console.WriteLine("The number you have chosen is {0}", userInput
}
public int InputCheck()
{
while (userInput >= 3)
{
while (userInput <= 10)
{
return userInput;
}
while (userInput > 10)
{
return defaulInt = userInput;
}
while (userInput < 3)
{
return defaultInt = userInput;
}
}
是这样甚至可以做一个同时循环? 我知道将它作为Switch做起来会更容易,但是我已经在这个程序中做了这个,并且需要实现一个While循环。
没有理由使用'while'。使用'if'(因为您默认为3而不要求重新输入)。你真正的错误是因为你不返回任何东西,如果'userInput <3' – Rob
我真的怀疑这个代码是你的老师的意思是“使用while循环”。你确定你不应该要求输入并重复评估它,直到用户输入一些值,如“退出”? – Blorgbeard