2017-10-19 41 views
1

Je travaille sur un programme pour ma classe C# qui est censé prendre un montant inscrit, comme un double ou non, et de trouver des changements en dollars, quartiers, etc.,Logique dans le programme de conversion des modifications | indexOutOfRange

J'utilise le Algorithme Greedy, et je continue à obtenir une sorte d'erreur qui se lit, "Une exception non gérée de type" System.IndexOutOfRangeException "s'est produite dans 2D.exe". Je suis encore relativement nouveau en C#, et viens d'un arrière-plan Java et C++.

Jusqu'à présent, j'ai ma classe d'argent:

using System; 
using static System.Console; 

namespace _2D 
{ 
    class Money 
    { 
    private double dollars, cents; 

    public void IncrementMoney() { } 
    public void DecrementMoney() { } 

    public Money(double dollarsncents) 
    { 
     double amountLeftOfDecimal = Math.Truncate(dollarsncents); 
     double amountRightOfDecimal = Math.Floor(dollarsncents); 

     this.dollars = Math.Round(amountLeftOfDecimal); 
     //the following LOGIC needs to be wokred out: 
     this.cents = Math.Round((amountRightOfDecimal * 100)/100, 2); 

    } 

    public Money(int ddollars, int ccents) 
    { 
     this.dollars = ddollars; 
     this.cents = ccents; 
    } 

    public override string ToString() 
    { 
     return String.Format(dollars + " dollars and " + cents + " cents."); 
    } 

    public void CoinAmounts(int inAmount, int remainder, int[] coins) 
    { 

     if((inAmount % 0.25) < inAmount) 
    { 
      coins[3] = (int)(inAmount/0.25); 
      remainder = inAmount % (1/4); 
      inAmount = remainder; 
     } 
     if ((inAmount % 0.1) < inAmount) 
     { 
      coins[2] = (int)(inAmount/0.1); 
      remainder = inAmount % (1/10); 
      inAmount = remainder; 
     } 
     if ((inAmount % 0.05) < inAmount) 
     { 
      coins[1] = (int)(inAmount/0.05); 
      remainder = inAmount % (1/20); 
      inAmount = remainder; 
     } 
     if ((inAmount % 0.01) < inAmount) 
     { 
      coins[0] = (int)(inAmount/0.01); 
      remainder = inAmount % (1/100); 
     } 
    } 
    public void PrintChange(int[] arr) 
    { 


     if (arr[3] > 0) 
      Console.WriteLine("Number of quarters: " + arr[3]); 
     if (arr[2] > 0) 
      Console.WriteLine("Number of dimes: " + arr[2]); 
     if (arr[1] > 0) 
      Console.WriteLine("Number of nickels: " + arr[1]); 
     if (arr[0] > 0) 
      Console.WriteLine("Number of pennies: " + arr[0]); 
    } 
} 

Et mon principal:

using System; 

namespace _2D 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      Money MyMoney = new Money(23, 24); 
      Console.WriteLine(MyMoney.ToString()); 

      Money dollarCentAmount = new Money(12.45); 
      Console.WriteLine(dollarCentAmount.ToString()); 
      Console.WriteLine("Press any key to continue."); 
      Console.ReadKey(); 
      Console.Clear(); 

      Console.WriteLine("Enter an amount you'd like change for: "); 
      double inAmountDouble = Convert.ToDouble(Console.ReadLine()); 
      int inAmount = Convert.ToInt32(inAmountDouble); 
      int tochange = inAmount; 
      int remainder = 0; 
      int[] coins = new int[3]; 

      MyMoney.CoinAmounts(inAmount, remainder, coins); 
      Console.WriteLine(" Change for " + inAmount + " is: "); 

      if (inAmount > 1.0) 
      { 
       Console.WriteLine("Number of dollars: " + Convert.ToInt32(inAmount)); 
      } 
      MyMoney.PrintChange(coins); 

      Console.ReadKey(); 
     } 
    } 
} 
+2

'int [] coins = new int [3]; '...' parts' ne contient que 3 'int's, donc' pièces de monnaie [3] = (int) (inAmount/0.25); 'est hors de portée car les index de tableau commencent à 0. –

+0

'pièces de monnaie 'devrait faire partie de la classe' Money', pas dans votre programme 'Main'. – NetMage

+0

pouvez-vous élaborer @johnnyMopp? –

Répondre

1

Vous avez déclaré des pièces pour être un tableau allant de 0 à 2

array[size] //size is how many elements are in the array, not the upper bound of the array 
coins[3] //means the array contains three elements, elements: 0, 1, 2 

//so you want: 
int[] coins = new int[4]; //Goes from 0 to 3, elements: 0, 1, 2, 3 

//This will allow you to later access: 
//since coins[3] is the 4th element, this is as high as the array can go now 
coins[3] = (int)(inAmount/0.25);