2014-09-03 62 views
0

可能是一个非常新的问题,但我们现在就去。类别/方法没有返回正确的值

我对此很新,现在我已经达到了我的第一个逻辑问题。

我创建了一个类+方法,它应该返回给我一个Int32值,它返回至少在我眼中无法访问的东西。我也不想要这个值被返回。

下面的代码:

public static Int32 SetInterval(ConsoleKeyInfo cki) 
    { 
     if (cki.Key == ConsoleKey.D1 || cki.Key == ConsoleKey.D2 || cki.Key == ConsoleKey.D3 || cki.Key == ConsoleKey.D4 || cki.Key == ConsoleKey.D5 || cki.Key == ConsoleKey.D6) 
     { 
      if (cki.Key == ConsoleKey.D1) 
      { 
       return 10000; 
      } 
      else if (cki.Key == ConsoleKey.D2) 
      { 
       return 20000; 
      } 
      else if (cki.Key == ConsoleKey.D3) 
      { 
       return 30000; 
      } 
      else if (cki.Key == ConsoleKey.D4) 
      { 
       return 45000; 
      } 
      else if (cki.Key == ConsoleKey.D5) 
      { 
       return 60000; 
      } 
      else if (cki.Key == ConsoleKey.D6) 
      { 
       return 120000; 
      } 
     } 
     else 
     { 
      SetInterval(Console.ReadKey()); 
     } 
     return 50; 
    } 

这里是我我的主类中执行它:

 static int interval; 

     interval = DefineInterval.SetInterval(Console.ReadKey()); 
     Console.WriteLine(""); 
     Console.WriteLine(interval.ToString()); 

所以什么,现在发生的事情是:

如果我按的一个6号码正确无需按任何其他键之前,它就好了。输出是正常的,正如它应该的那样。

话又说回来,当我按下,例如 “A6” 在我的键盘我得到的是:

“ A6 ”

任何想法?也可能不是做这种事情的最佳方式。

+1

这看起来像我期望的输出......你打算从'else'块返回SetInterval(Console.ReadKey())吗? – 2014-09-03 17:08:07

+0

你可以删除你的外部if语句,它不会做任何事情 – Jonesopolis 2014-09-03 17:08:49

+0

也考虑使用'switch'语句。 – arao6 2014-09-03 17:09:22

回答

2

elseSetInterval递归调用并没有对返回任何有价值的东西。你想要的是这样的:

public static Int32 SetInterval(ConsoleKeyInfo cki) 
{ 
    if (cki.Key == ConsoleKey.D1) 
    { 
     return 10000; 
    } 
    else if (cki.Key == ConsoleKey.D2) 
    { 
     return 20000; 
    } 
    else if (cki.Key == ConsoleKey.D3) 
    { 
     return 30000; 
    } 
    else if (cki.Key == ConsoleKey.D4) 
    { 
     return 45000; 
    } 
    else if (cki.Key == ConsoleKey.D5) 
    { 
     return 60000; 
    } 
    else if (cki.Key == ConsoleKey.D6) 
    { 
     return 120000; 
    } 
    else 
    { 
     return SetInterval(Console.ReadKey()); 
    } 
} 

请注意,我也动了不必要的if语句包围第一否则,如果链。

0
public static Int32 SetInterval(ConsoleKeyInfo cki) 
{ 
    if (cki.Key == ConsoleKey.D1 || cki.Key == ConsoleKey.D2 || cki.Key == ConsoleKey.D3 || cki.Key == ConsoleKey.D4 || cki.Key == ConsoleKey.D5 || cki.Key == ConsoleKey.D6) 
    { 
     if (cki.Key == ConsoleKey.D1) 
     { 
      return 10000; 
     } 
     else if (cki.Key == ConsoleKey.D2) 
     { 
      return 20000; 
     } 
     else if (cki.Key == ConsoleKey.D3) 
     { 
      return 30000; 
     } 
     else if (cki.Key == ConsoleKey.D4) 
     { 
      return 45000; 
     } 
     else if (cki.Key == ConsoleKey.D5) 
     { 
      return 60000; 
     } 
     else if (cki.Key == ConsoleKey.D6) 
     { 
      return 120000; 
     } 
    } 
    else 
    { 
     return SetInterval(Console.ReadKey()); 
    } 
} 
0

您正在递归调用您的方法。当你给出正确的输入时,它会很好。

否则它会得到正确的输入,然后正确的输入将返回方法第一次调用的默认值。

解决方案:

只需删除设置return 50和:

else 
{ 
    return SetInterval(Console.ReadKey()); 
} 
-1

快速问题 - 为什么你会按“a6”?是错误的,你希望这个被你的程序只读为6并继续前进,还是有其他原因背后的原因?如果这是原因,那么你需要删除返回50,并将其返回SetInterval(Console.ReadKey())。