计算和显示方法的问题

时间:2016-02-05 16:19:35

标签: c# if-statement methods try-catch

我遇到了编程任务的问题。老师给我们分配了以下内容:

从用户那里获取有关购买总额及其提交金额的输入,然后从主例程中调用单个方法来计算并显示结果(结果是用户在百元钞票中获得的更改,五十美元钞票等。

我有这个问题的可行代码,但我知道我没有满足要求。我也有一种“直觉”,有一种更好的方式来编写代码,使其不那么笨重。我正在寻找有关如何改进我所拥有的代码的建议。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Change
{
    class Change
    {
        static void Main(string[] args)
        {
            //Initialize variables
            double costOfPurchase = 0;
            double amountSubmitted = 0;
            double hundredDollarBills = 0;
            double fiftyDollarBills = 0;
            double twentyDollarBills = 0;
            double fiveDollarBills = 0;
            double quarters = 0;
            double nickles = 0;
            double pennies = 0;

            //Set up try catch for errors from user input
            try
            {
                //Get the cost of a sale
                Console.Write("Enter the total cost of your purchase: ");
                costOfPurchase = Convert.ToDouble(Console.ReadLine());

                if (costOfPurchase < 0)
                {
                    Console.WriteLine("The cost of your purchase cannot be a negative value.");
                }

                //Get the amount submitted to pay for the sale
                Console.Write("Enter the amount submitted: ");
                amountSubmitted = Convert.ToDouble(Console.ReadLine());

               if (costOfPurchase > amountSubmitted)
                {
                    Console.WriteLine("You have not submitted enough money.");
                }

                //Call a method to calculate and display the change due
                calculateChangeDue(costOfPurchase, amountSubmitted, out hundredDollarBills, out fiftyDollarBills, out twentyDollarBills, out fiveDollarBills, out quarters, out nickles, out pennies);

                Console.WriteLine(hundredDollarBills + " Hundred(s)");
                Console.WriteLine(fiftyDollarBills + " Fifty");
                Console.WriteLine(twentyDollarBills + " Twenty(s)");
                Console.WriteLine(fiveDollarBills + " Five");
                Console.WriteLine(quarters + " Quarter(s)");
                Console.WriteLine(nickles + " Nickle");
                Console.WriteLine(pennies + " Penny(s)");

                //Keep the console open in debug mode
                Console.Write("Press any key to exit.");
                Console.ReadKey();
            }
            catch (System.Exception e)
            {
                Console.WriteLine("An error has occurred.");
                Console.WriteLine("The eror was: {0}", e.Message);
                Console.ReadLine();
            }
        }

        //Method to calculate and display the change due
        private static void calculateChangeDue(double costOfPurchase, double amountSubmitted, out double hundredDollarBills, out double fiftyDollarBills, out double twentyDollarBills, out double fiveDollarBills, out double quarters, out double nickles, out double pennies)
        {
            //Initialize variables
            double changeDue = 0;
            double remainingChange = 0;

            //Calculate the change due
            changeDue = amountSubmitted - costOfPurchase;

            remainingChange = changeDue % 100;
            hundredDollarBills = (changeDue / 100) - (remainingChange / 100);

            fiftyDollarBills = (remainingChange / 50) - ((remainingChange % 50) / 50);
            remainingChange = remainingChange % 50;

            twentyDollarBills = (remainingChange / 20) - ((remainingChange % 20) / 20);
            remainingChange = remainingChange % 20;

            fiveDollarBills = (remainingChange / 5) - ((remainingChange % 5) / 5);
            remainingChange = remainingChange % 5;

            quarters = (remainingChange / 0.25) - ((remainingChange % 0.25) / 0.25);
            remainingChange = remainingChange % 0.25;

            nickles = (remainingChange / 0.05) - ((remainingChange % 0.05) / 0.05);
            remainingChange = remainingChange % 0.05;

            pennies = (remainingChange / 0.01) - ((remainingChange % 0.01) / 0.01);
        }
    }
}

0 个答案:

没有答案