我的程序應該顯示需要多少個月才能償還債務。我的問題是,當我把代碼解決了需要多長時間來償還債務的方法,我的價值回到了負面。我的公式代碼是正確的,其他所有功能都正常運行,只是當我聲明CreditCardMonths() 來解決問題時,無論我的輸入是什麼,我總是得到-2763737號碼。C#方法返回值錯誤負數
這裏是我的代碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DebtCalculator
{
class Program
{
static void Main(string[] args)
{
char menuItem;
Console.WriteLine("Welcome to the credit card balance payoff calculator");
Console.WriteLine();
intro();
CreditCardMonths();
displaymenu();
menuItem = GetUpperCaseYN();
while (menuItem != 'N')
{
ProcessMenuItem(menuItem);
menuItem = GetUpperCaseYN();
}
Console.WriteLine("Thank you, goodbye.");
Console.ReadKey();
}
static void intro()
{
double creditcardbalance;
creditcardbalance = GetPositiveDouble("Enter In Credit Card Balance");
double monthlypayementamount;
monthlypayementamount = GetPositiveDouble("Enter In Monthly Payement Amount");
decimal percentagerate;
percentagerate = GetPositiveDecimal("Enter In Percentage Rate");
}
static void CreditCardMonths()
{
double dailyinterestrate;
double payoff;
double creditcardbalance = 0;
double monthlypayementamount = 0;
decimal percentagerate = 0;
dailyinterestrate = (double)percentagerate/365;
payoff = (int)(-(1d/30) * Math.Log(1 + creditcardbalance/monthlypayementamount * (1 - Math.Pow(1 + dailyinterestrate, 30)))/Math.Log(1 + dailyinterestrate)) + 1;
Console.WriteLine("It will take {0} months to pay off debt",payoff);
}
//}
static decimal GetPositiveDecimal(string message)
{
decimal value;
Console.WriteLine(message);
value = decimal.Parse(Console.ReadLine());
while (value <= 0)
{
Console.WriteLine("invalid input. Please enter a non negative Decimal");
Console.WriteLine(message);
value = decimal.Parse(Console.ReadLine());
}
return value;
}
static double GetPositiveDouble(string message)
{
double value;
Console.WriteLine(message);
value = double.Parse(Console.ReadLine());
while (value <= 0)
{
Console.WriteLine("Invalid Input . Enter In A Non Negative Number");
Console.WriteLine(message);
value = double.Parse(Console.ReadLine());
}
return value;
}
static char GetUpperCaseYN()
{
char menuItem;
menuItem = char.Parse(Console.ReadLine());
menuItem = char.ToUpper(menuItem);
while (menuItem != 'Y' && menuItem != 'N')
{
Console.WriteLine("Invalid Menu Item");
menuItem = char.Parse(Console.ReadLine());
menuItem = char.ToUpper(menuItem);
}
return menuItem;
}
static void displaymenu()
{
Console.WriteLine("Calculate Another? (Y/N)");
}
static void ProcessMenuItem(char menuItem)
{
switch (menuItem)
{
case 'Y':
intro();
displaymenu();
break;
}
}
}
}
請提供一個簡潔的程序來重現您的問題。 [如何提出一個好問題](http://stackoverflow.com/help/how-to-ask) –