static void Main(string[] args)
{
double creditcardbalance;
creditcardbalance = GetPositiveDouble("Enter In Credit Card Balance");
double monthlypayementamount;
monthlypayementamount = GetPositiveDouble("Enter In Monthly Payement Amount");
double percentagerate;
percentagerate = (int)GetPositiveDecimal("Enter In Percentage Rate");
CreditCardMonths();
}
static void CreditCardMonths()
{
double dailyinterestrate;
double payoff;
double percentagerate = 0;
double creditcardbalance = 0;
double monthlypayementamount = 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 the loan", payoff);
displaymenu();
Console.ReadKey();
}
一旦在主方法中聲明瞭方法CreditCardMonths(),我的公式的答案返回一個負整數,當它應該返回一個正數。 我在想什麼?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)
{
double creditcardbalance;
creditcardbalance = GetPositiveDouble("Enter In Credit Card Balance");
double monthlypayementamount;
monthlypayementamount = GetPositiveDouble("Enter In Monthly Payement Amount");
double percentagerate;
percentagerate = (int)GetPositiveDecimal("Enter In Percentage Rate");
CreditCardMonths();
}
這是一段代碼的工作原理:
double creditcardbalance;
Console.WriteLine("Enter in credit card balance:");
creditcardbalance = double.Parse(Console.ReadLine());
double monthlypayementamount;
Console.WriteLine("Enter in the monthly payement amount:");
monthlypayementamount = double.Parse(Console.ReadLine());
double percentagerate;
Console.WriteLine("Enter in annual percentage rate:");
percentagerate = double.Parse(Console.ReadLine());
// outputs
double payoff;
double dailyinterestrate;
// Daily interest formula calculated by apr divided by 365
dailyinterestrate = percentagerate/365;
//Introducing formula for output
//Payoff needed to display whole numbers, although in ints output 1/30 = 0 so adding 1d (double) allows precise calculation.+1 allows it to round up to the nearest dollar.
payoff = (int)(-(1d/30) * Math.Log(1 + creditcardbalance/monthlypayementamount * (1 - Math.Pow(1 + dailyinterestrate, 30)))/Math.Log(1 + dailyinterestrate)) + 1;
//Introduce output for display
{
Console.WriteLine("It will take {0} months to pay off the loan", payoff);
Console.WriteLine("Goodbye");
// keep console open
Console.ReadKey();
但似乎通過創建一個名爲CreditCardMonths的方法返回一個負整數? 另外我的Y/N for循環方法不起作用。
static void CreditCardMonths()
{
double dailyinterestrate;
double payoff;
double percentagerate = 0;
double creditcardbalance = 0;
double monthlypayementamount = 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 the loan", payoff);
displaymenu();
Console.ReadKey();
}
static decimal GetPositiveDecimal(string message)
{
decimal value = 0;
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 = 0;
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 void menuItem()
{
char menuItem;
{
menuItem = GetUpperCaseYN();
ProcessMenuItem(menuItem);
}
Console.WriteLine("Thank you, goodbye.");
Console.ReadLine();
}
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':
;
break;
case 'N':
Console.ReadKey();
Console.WriteLine("Goodbye!");
Console.ReadKey();
break;
}
Console.ReadKey();
}
}
}
把你CreditCardMonths方法中設置斷點,並通過它檢查值步驟。你應該能夠找到你的問題。還要確保你沒有聲明一個局部範圍的變量,它掩蓋了一個方法/類範圍的 –