C# 通过询问用户计算罚款';输入值
任务是编写一个程序,在计算罚款时询问用户书籍的数量和天数 一些规则:C# 通过询问用户计算罚款';输入值,c#,C#,任务是编写一个程序,在计算罚款时询问用户书籍的数量和天数 一些规则: 用户不能有超过五本书 如果天数少于21天,则无需罚款 如果天数在22-30天之间,则每天每本书罚款50美分 她已经超过21岁了 如果天数超过21天,每本书每天罚款80美分,并显示“会员资格取消”信息 一切似乎都很正常,除了开始时如果值大于5或小于5,它会询问两倍的图书数量,在询问输入值几天后,它会再次询问图书数量 class Program { public static int GetBook(int
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));
}
}
类程序
{
公共静态int-GetBook(int-book)
{
bool bookAmount=真;
如果(书籍<5)
{
还书;
}
其他的
{
做
{
Console.WriteLine(“输入图书数量:”);
book=Int32.Parse(Console.ReadLine());
如果(书籍>5)
{
Console.WriteLine(“无效值,最大图书数为5”);
}
其他的
{
账面金额=假;
}
}而(账面金额);
还书;
}
}
公共静态整数天(整数天)
{
返程天数;
}
公共静态双转换罚款(整数天,整数本)
{
天=GetDays(天);
book=GetBook(book);
双倍罚款=0;
如果(days=22&&days在计算罚款时再次调用GetBook
。请在ConvertFine
方法中删除对GetBook和GetDays的调用
您还可以稍微简化GetBook:
public static int GetBook(int book)
{
if (book <= 5)
{
return book;
}
do
{
Console.WriteLine("Not valid value, max number of books is 5");
Console.WriteLine("Enter the number of books: ");
book = Int32.Parse(Console.ReadLine());
} while (book > 5);
return book;
}
public static int GetDays(int days)
{
return days;
}
public static double ConvertFine(int days, int 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;
}
publicstaticintgetbook(intbook)
{
如果(第5册);
还书;
}
公共静态整数天(整数天)
{
返程天数;
}
公共静态双转换罚款(整数天,整数本)
{
双倍罚款=0;
如果(days=22&&days)在两个位置调用GetBook方法,这是有意的吗?