2016-11-18 45 views
-3

对于我正在尝试编写的程序,我需要同时使用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循环。

+0

没有理由使用'while'。使用'if'(因为您默认为3而不要求重新输入)。你真正的错误是因为你不返回任何东西,如果'userInput <3' – Rob

+2

我真的怀疑这个代码是你的老师的意思是“使用while循环”。你确定你不应该要求输入并重复评估它,直到用户输入一些值,如“退出”? – Blorgbeard

回答

0

if声明实际上是你在找什么,不while
谈到if使用,代码便利性和可读性,我还要重新安排它是这样的:

public int InputCheck() 
{ 
    if (userInput > 10) 
     return defaultInt = userInput; 

    if (userInput >= 3) 
     return userInput; 

    return defaultInt = userInput; 
} 

看,它现在看起来更好,但做同样的事情。
您的if条件中的两个实际上是多余的,因为根据之前的条件,它们保证为true

然而,你的代码还是非工作...

  1. 那是什么:defaultInt = userInput?你真的想重写defaultInt的值吗?名称默认表明它不应该。
  2. 此外,您尝试访问另一种方法中的局部变量,这是不可能的。
  3. 你缺少一对圆括号和花括号。

请务必仔细阅读,并要求在下一个问题之前,了解这种材料:

+0

是的,我认为这看起来好多了!使用你的代码,我可以通过添加一个上限“if(userHeightInput <= 10)” – user7176008

-1
public int InputCheck() 
{ 
    if (userInput >= 3) 
    { 
     if (userInput <= 10) 
     { 
      return userInput; 
     } 
     if (userInput > 10) 
     { 
      return defaulInt = userInput; 
     } 
     } 
    if (userInput < 3) 
    { 
     return defaultInt = userInput; 
    } 
} 
+0

我的IDE会提示两个'if's是多余的:) –

+0

这不会编译 - 它与标题中的错误完全相同。 – Rob

0

我觉得牛逼他的想法while循环在你的情况是保持程序要求userInput,直到它对输入值感到满意为止。但是,您有一个while循环,但是您的功能无法解决不正确的值userInput

我重新安排了一点逻辑,以便用户有机会修复while循环内的输入错误。

static void Main() 
    { 
     // `userInput` and `defaultInt` are only visible inside this function 
     // so you won't have access to them in InputCheck() unless you pass them in as parameters ... 

     int userInput = -1; //user enters int with ReadLine 
     int defaultInt = 3; //user keys anything other than 3 through 10 
     bool ok = false;  // flag to keep you in the while loop until the userInput is acceptable 

     while (!ok) // while we are *not* ok - the while-condition is checked at the top of the while-loop 
     { 
      // Give user a chance to fix `userInput` inside the while loop 
      Console.WriteLine("Enter an Integer from 3 to 10"); // let the user know what the valid inputs are 
      userInput = int.Parse(Console.ReadLine());    // need to fix!! if user enters non-integer, eg "quit", this will throw an exception 

      ok = InputCheck(userInput); 
      // You have a choice here: 
      // You can keep looping until user specifies a number from 3 to 10 

      // Or you can simply override a bad choice with your default. 
      // If you don't want the default logic, get rid of the following block 
      if (!ok) 
      { 
       // Let the user know what you're doing 
       Console.WriteLine("Overriding your invalid choice {0} to {1}", userInput, defaultInt); 
       userInput = defaultInt; // override user's choice 
       ok = true;     // force the while loop to exit 
      } 

     } 
     Console.WriteLine("The number you have chosen is {0}", userInput); 
    } 

    // Move the while loop out of `InputCheck` function. 
    // InputCheck now has only one job - to check that userInput is acceptable 

    public bool InputCheck(int userInput) // you have to pass `userInput` in as parameter - it is only visible in Main() 
    { 
     if (3 <= userInput && userInput <= 10) 
      return true; 
     else 
      return false; 
    } 
} 
相关问题