任务是编写一个程序,该程序在计算正常的同时询问用户书本和天数。
一些规则:
一切似乎都可以正常工作,除了在开始时要求输入两倍的书本值(如果值大于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));
}
}
答案 0 :(得分:0)
您在计算罚款时再次致电fromGraph :: Eq a => [(a, b)] -> a -> b
fromGraph g i = snd $ head $ filter ((==i) . fst) g
。用GetBook
方法删除对GetBook和GetDays的调用。
您还可以稍微简化GetBook:
ConvertFine