【问题标题】:Class / Method not returning the correct value类/方法没有返回正确的值
【发布时间】:2014-09-03 17:03:09
【问题描述】:

可能是一个相当新的问题,但我们开始吧。

我对此很陌生,现在我想我已经遇到了我的第一个逻辑问题。

我创建了一个类 + 方法,它应该返回一个 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 50 "

有什么想法吗?也可能不是做这种事情的最佳方式。

【问题讨论】:

  • 这看起来像我期望的输出......你打算从 else 块中 return SetInterval(Console.ReadKey()) 吗?
  • 你可以删除你的外部 if 语句,它没有做任何事情
  • 考虑使用switch 语句。

标签: c# console console-application windows-console


【解决方案1】:

else 块中对SetInterval 的递归调用不会对返回值做任何事情。你想要的是这样的:

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

请注意,我还移动了围绕第一个 else if 链的不必要的 if 语句。

【讨论】:

    【解决方案2】:
    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());
        }
    }
    

    【讨论】:

      【解决方案3】:

      您正在递归调用您的方法。当您提供正确的输入时,一切正常。

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

      解决方案:

      只需删除return 50 并设置:

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

      【讨论】:

        【解决方案4】:

        快速提问 - 为什么要按“a6”?这是错误的,您希望程序仅将其读取为 6 并继续前进,还是背后有其他原因?如果这是原因,那么您需要删除 return 50 并将 return SetInterval(Console.ReadKey()) 放在 else 中。

        【讨论】:

          猜你喜欢
          • 2022-10-14
          • 1970-01-01
          • 1970-01-01
          • 2018-07-09
          • 2021-09-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多