[英]Convert 1000 months in 83 years and 4 months
I try to make something and its work, but when I try to make a date under 1 month its crash. 我尝试做某事及其工作,但是当我尝试在1个月以下约会时,它就会崩溃。 Can someone help me? 有人能帮我吗? The code is like this: 代码是这样的:
int page = int.Parse(Console.ReadLine());
int campingDays = int.Parse(Console.ReadLine());
int pagesPerDay = int.Parse(Console.ReadLine());
int months = page / ((30 - campingDays) * pagesPerDay);
int years = months / 12;
int remainingMonths = months % 12;
Console.WriteLine("{0} years {1} months", years, remainingMonths);
That's how i solve the "0 years 1 months" problem. 这就是我解决“ 0年1个月”问题的方式。 There you go! 你去!
int readedPages = (30 - campingDays) * regularDays;
if ((campingDays == 30) || (regularDays == 0))
{
Console.WriteLine("never");
}
else
{
double readDuration = Math.Ceiling(bookPages / (double)readedPages);
double years = (int)(readDuration / 12);
double remainingMonths =(readDuration % 12);
Console.WriteLine("{0} years {1} months", years, remainingMonths);
}
You would get divide by zero error whenever 每当你会得到除以零的错误
campingDays
is 30 because of (30 - campingDays)
campingDays
是30,因为(30 - campingDays)
pagesPerDay
is - 0 pagesPerDay
是-0 The problematic line: 有问题的行:
int months = page / ((30 - campingDays) * pagesPerDay);
You may want 2 if
statements to handle those cases. 您可能需要2条if
语句来处理这些情况。
Michal Ciechan is correct. Michal Ciechan是正确的。 (Just read your answer closely) (请仔细阅读您的答案)
suggestion: 建议:
int _campingDaysAdj = 30 - campingDays;
if(_campingDaysAdj > 0 && pagesPerDay > 0)
{
int months = page / ((30 - campingDays) * pagesPerDay);
int years = months / 12;
int remainingMonths = months % 12;
Console.WriteLine("{0} years {1} months", years, remainingMonths);
}
else
{
//throw an exception or an error message etc.
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.