【问题标题】:Why is my method name giving me an error?为什么我的方法名称给我一个错误?
【发布时间】:2019-03-25 20:55:21
【问题描述】:

我的方法 CalculateFee 给了我一个错误。只是名称“CalculateFee”下的一条红线

我尝试将公共更改为私人,但没有帮助,甚至无效,即使这对我来说有点愚蠢......

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FineForOverdueBooks
{                   
    class Program   
    {               
        static void Main(string[] args)
        {
            Console.WriteLine("How many books were checked out?");
                int booksCheckedOut = Convert.ToInt32(Console.ReadLine());
            {
                if (booksCheckedOut == 1)
                {
                    Console.WriteLine("How many days is it overdue?");
                }
                else
                    Console.WriteLine("How many days are they overdue?");
            }
            int daysOverdue = Convert.ToInt32(Console.ReadLine());
            int output = CalculateFee(booksCheckedOut, daysOverdue);
            Console.WriteLine("Your late fee is: ", output.ToString("C"));
        }

    public static int **CalculateFee**(int booksCheckedOut, int daysOverdue)
    {
        double libraryFine = .10;
        double additionalFine = .20;
        if (daysOverdue <= 7)
        {
            for (double x = 0; x <= 7; x++)
            {
                libraryFine += .10;
            }
            return Convert.ToInt32(libraryFine);
        }
        if (daysOverdue > 7)
        {
            for (int x =0; x<=daysOverdue; x++)
            {
                additionalFine += .20;
            }
            return Convert.ToInt32(additionalFine);
        }
    }
}
}

我只是希望我的方法能够执行,以便查看我的 for 循环是否正确以及是否需要更改一些内容(我可能会这样做)

抱歉,如果这看起来是重复的,我找不到与我的问题直接相关的任何内容。

【问题讨论】:

  • 你得到什么错误?
  • 并非所有代码路径都返回值
  • 在@ZoharPeled类里面,只是缩进错了。
  • 您有错误,因为您有 2 个 if。因此,您可以将第二个 if 替换为 else 或在方法的末尾放置另一个 return,以便所有路径都返回一个值。
  • @nbokmans 是的,缩进让我很困惑。一旦意识到这一点,我就删除了我的评论。

标签: c# console-application


【解决方案1】:

因为CalculateFee 并非在所有情况下都返回。实际上在这种情况下,它只是不会静态分析所有值以知道它永远无法到达那里。考虑使用else

public static int CalculateFee(int booksCheckedOut, int daysOverdue)
{
   double libraryFine = .10;
   double additionalFine = .20;
   if (daysOverdue <= 7)
   {
      for (double x = 0; x <= 7; x++)
      {
         libraryFine += .10;
      }
      return Convert.ToInt32(libraryFine);
   }
   else
   {
      for (int x =0; x<=daysOverdue; x++)
      {
         additionalFine += .20;
      }
      return Convert.ToInt32(additionalFine);
   }

}

【讨论】:

  • 实际上,它在所有情况下都会返回,只是编译器没有赶上。如果 int 不小于或等于 7,则它必须大于 7。正确的做法是删除第二个条件或将其更改为第一个条件的 else 块。
  • 基本上,编译器不够聪明,无法意识到&lt;=7&gt;7 涵盖了所有可能的情况(并且您不会更改daysOverdue)。修复方法是将第二个 if 替换为 else
  • @ZoharPeled 编译器无法确定daysOverdue 的值不会在两个ifs 之间改变。
  • @mjwills 它如何改变它的价值?它是一个值类型参数,所以它是线程安全的,并且代码中没有任何东西在改变它......
  • @ZoharPeled How can it change it's value? 在调试器中? ;) 为了让它接受此代码,它需要进行静态分析以查看没有任何东西在改变它的值并且不允许您在调试器中更改它。这不是 Robin 声称的“聪明”问题,我不认为 - 它实际上是变得聪明,因为它知道它无法控制什么(因为它不是const).
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-10
  • 1970-01-01
  • 2011-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多