根据询问用户的输入值来计算罚款

时间:2020-10-30 11:20:30

标签: c#

任务是编写一个程序,该程序在计算正常的同时询问用户书本和天数。

一些规则:

  1. 用户的书籍不能超过五本
  2. 如果天数少于21天,则不会罚款
  3. 如果天数在22 – 30之间,则每天每本书每天罚款50美分 超过21
  4. 如果天数超过21天,则每天每本书每天罚款80美分,并显示“会员资格已取消”消息

一切似乎都可以正常工作,除了在开始时要求输入两倍的书本值(如果值大于5或小于5)之后,连续几天询问输入值后,它会再次询问书本数量。

class Program
    {
        public static int GetBook(int book)
        {
            bool bookAmount = true;
            if (book < 5)
            {
                return book;
            }
            else
            {
              do
              {
                  Console.WriteLine("Enter the number of books: ");
                  book = Int32.Parse(Console.ReadLine());
                  if (book > 5)
                  {
                      Console.WriteLine("Not valid value, max number of books is 5");
                  }
                  else
                  {
                      bookAmount = false;
                  }
              } while (bookAmount);
              return book;  
            }
            
        }

        public static int GetDays(int days)
        {
            return days;
        }

        public static double ConvertFine(int days, int book)
        {
            days = GetDays(days);
            book = GetBook(book);
            double fine = 0;
            
            if (days <= 21)
            {
                Console.WriteLine("Fine is: " + fine);
            }
            else if (days >= 22 && days <= 30)
            {
                fine += (days - 21) * 0.5 * book;
                Console.WriteLine("Fine is: " + fine + " euros");
            }
            else
            {
                fine += (days - 21) * 0.8* book;
                Console.WriteLine("Membership cancelled!");
                Console.WriteLine("Fine is: " + fine + " euros");
            }
            return fine;
        }
        static void Main(string[] args)
        {
            Console.WriteLine("Enter the number of books: ");
            int book = Int32.Parse(Console.ReadLine());
            Console.WriteLine(GetBook(book));
            
            Console.WriteLine("Enter the number of days: ");
            int days = Int32.Parse(Console.ReadLine());
            Console.WriteLine(GetDays(days));
            
            Console.WriteLine(ConvertFine(days, book));
        }
    }

1 个答案:

答案 0 :(得分:0)

您在计算罚款时再次致电fromGraph :: Eq a => [(a, b)] -> a -> b fromGraph g i = snd $ head $ filter ((==i) . fst) g 。用GetBook方法删除对GetBook和GetDays的调用。

您还可以稍微简化GetBook:

ConvertFine