嵌套While循环,其中“并非所有代码路径都返回值”

时间:2016-11-18 02:48:13

标签: c# while-loop nested

对于我尝试编写的程序,我需要同时使用Switch case和While循环。我对Switch示例没有任何问题,但是我不确定如何使用While循环获得类似的解决方案。

我试图用嵌套的while循环评估用户输入的值。 在其中我想让用户“选择”3到10之间的任何数字。(假设使用System等都是正确的)

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;
     }
}

这样的事情甚至可以用while循环吗? 我知道作为Switch更容易做到这一点,但我已经在这个程序中完成了这个并且需要实现一个While循环。

3 个答案:

答案 0 :(得分: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. 你缺少几个括号和花括号。
  4. 在询问下一个问题之前,请务必完整阅读并理解这些材料:

答案 1 :(得分: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;
    }
}

答案 2 :(得分:-1)

$a = array_merge($a, array('email' => $this->mail, 'votes' => $this->votes));