2015-11-06 118 views
-3

我的程序應該顯示需要多少個月才能償還債務。我的問題是,當我把代碼解決了需要多長時間來償還債務的方法,我的價值回到了負面。我的公式代碼是正確的,其他所有功能都正常運行,只是當我聲明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; 
      } 
     } 
    } 
} 
+0

請提供一個簡潔的程序來重現您的問題。 [如何提出一個好問題](http://stackoverflow.com/help/how-to-ask) –

回答

0

的問題是,該方法沒有您鍵入的值的想法

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"); 

     // Everything available until here! 
    } 

    static void CreditCardMonths() 
    { 

     double dailyinterestrate; 
     double payoff; 
     double creditcardbalance = 0; 
     double monthlypayementamount = 0; 
     decimal percentagerate = 0; 

     // values set to what you wrote above (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 double creditcardbalance; 
// your variables..  

static void Main(string[] args) 
{ 
    // Call your methods 
} 

static void intro() 
{ 
     creditcardbalance = GetPositiveDouble("Enter In Credit Card Balance"); 
     monthlypayementamount = GetPositiveDouble("Enter In Monthly Payement Amount"); 
     percentagerate = GetPositiveDecimal("Enter In Percentage Rate"); 
} 
static 
    ... // your methods 

如果您願意使用參數(我通常做的),它看起來就像是: (例如)

static void Main(string[] args) 
{ 
    //call your methods 
} 

static void CreditCardMonths(double creditcardbalance, double monthlypayementamount, decimal percentagerate) 
{ 

     double dailyinterestrate; 
     double payoff; 

     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); 

    } 

    //Call it like CreditCardMonths(yourValue0, yourValue1, yourValue2); 

0

你得到相同的結果的原因,所有的時間,因爲你重新聲明變量

double creditcardbalance = 0; 
double monthlypayementamount = 0; 
decimal percentagerate = 0; 

您必須傳遞這些值,並從使用和設置它們的各種方法。