为什么我的方法名称给我一个错误?

时间:2019-03-25 12:16:39

标签: c# console-application

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

我曾尝试将公共场所更改为私人场所,但这无济于事,即使将int设置为无效对我来说都是愚蠢的……

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循环以及是否需要更改某些东西(我可能这样做了)

抱歉,如果这看起来像重复的话,我找不到与我的问题直接相关的任何东西。

1 个答案:

答案 0 :(得分:4)

因为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);
   }

}