2017-10-19 3 views
1

Ich arbeite an einem Programm für meine C# -Klasse, die angeblich eine eingegebene Menge zu nehmen, als Doppel- oder nicht, und die Änderung des US-Dollar zu finden, Viertel usw.Logic in Change Konvertierungsprogramm | indexOutOfRange

ich bin mit Greedy Algorithmus, und ich bekomme immer eine Art von Fehler, der lautet: "Eine nicht behandelte Ausnahme vom Typ 'System.IndexOutOfRangeException' in 2D.exe aufgetreten".

Ich bin noch relativ neu in C# und komme aus einem Java und C++ Hintergrund.

Bisher habe ich mein Geld Klasse:

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

Und mein Haupt:

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 sein würde [] coins = new int [3]; '...' coins' enthält nur 3 'int's, also' coins [3] = (int) (inAmount/0.25); 'liegt außerhalb des Bereichs, da Array-Indizes beginnen bei 0. –

+0

'Münzen' sollten Teil der' Money' Klasse sein, nicht in Ihrem 'Main' Programm. – NetMage

+0

können Sie @johnnyMopp ausarbeiten? –

Antwort

1

Sie Münzen deklariert ein Array von 0 bis 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);