Почему имя моего метода выдает ошибку? - PullRequest
0 голосов
/ 25 марта 2019

Мой метод 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 Ответ

4 голосов
/ 25 марта 2019

Потому что 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);
   }

}
...