变革转换计划中的逻辑| indexOutOfRange

时间:2017-10-19 19:30:32

标签: c# math type-conversion logic

我正在为我的C#课程开发一个程序,该程序应该输入一个输入的数量,不管是否加倍,并找到美元,四分之一等的变化,

我正在使用贪婪算法,并且我不断收到某种类型的错误,"未处理的类型' System.IndexOutOfRangeException'发生在2D.exe"。

我还是比较新的C#,来自Java和C ++背景。

到目前为止,我有我的Money课程:

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

我的主要:

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

1 个答案:

答案 0 :(得分:1)

你宣称硬币是一个从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);