简体   繁体   中英

Adding User Defined data

First thing I want to say is that this is for my homework. I am not looking for the answer just assistance in figuring out what I am doing wrong. This is my fist programming class and up till this point I have been doing great. Now all of a sudden I am lost.

The Question: Create an application that lets the user enter the monthly costs of the following expenses incurred from operating his or her automobile: Loan payment, insurance, gas, oil, tires, and maintenance. The program should then display the total monthly cost of these expenses and the total annual cost of these expenses.

This is what I have built:

private void totalMonthlyButton_Click(object sender, EventArgs e) {

        decimal loan;        // Monthly cost of loan
        decimal insurance;   // Monthly insurance cost
        decimal gas;         // Monthly gas cost
        decimal oil;         // Monthly oil cost
        decimal tires;       // Monthly tire cost
        decimal maintenance; // Monthly maintenance cost
        decimal monthlyCost; // Monthly total cost

        // Get the loan amount.
        loan = decimal.Parse(loanTextBox.Text);
        // Get the insurance amount.
        insurance = decimal.Parse(insTextBox.Text);
        // Get the gas amount.
        gas = decimal.Parse(gasTextBox.Text);
        // Get the oil amount.
        oil = decimal.Parse(oilTextBox.Text);
        // get the tires amount.
        tires = decimal.Parse(tiresTextBox.Text);
        // Get the maintenance amount.
        maintenance = decimal.Parse(mainTextBox.Text);
        // determine the monthly cost.
        monthlyCost = decimal.Parse(totalMonthlyLabel.Text);

        // Calculate monthly cost.
        monthlyCost = loan + insurance + gas + oil + tires + maintenance;

        // Display the monthlyCost in the correct control.

Any help on this is greatly appreciated. Again I do not want the answer just some pointers of if I'm heading in the right direction, if I am completely off, where to go next etc.

Thank you everyone.

This is the best way that I can think of right now:

decimal loan;        // Monthly cost of loan
decimal insurance;   // Monthly insurance cost
decimal gas;         // Monthly gas cost
decimal oil;         // Monthly oil cost
decimal tires;       // Monthly tire cost
decimal maintenance; // Monthly maintenance cost
decimal monthlyCost; // Monthly total cost

try
{
    loan = decimal.Parse(loanTextBox.Text);
    insurance = decimal.Parse(insTextBox.Text);
    gas = decimal.Parse(gasTextBox.Text);
    oil = decimal.Parse(oilTextBox.Text);
    tires = decimal.Parse(tiresTextBox.Text);
    maintenance = decimal.Parse(mainTextBox.Text);
    monthlyCost = loan + insurance + gas + oil + tires + maintenance;
    TotalMonthlyLabel.Text = monthlyCost.ToString();// Display the monthlyCost in the correct control.
}
catch { }

Try this, I removed a variable because that variable does not read the value it just has to display the value.

        decimal loan;        // Monthly cost of loan
        decimal insurance;   // Monthly insurance cost
        decimal gas;         // Monthly gas cost
        decimal oil;         // Monthly oil cost
        decimal tires;       // Monthly tire cost
        decimal maintenance; // Monthly maintenance cost
        decimal monthlyCost; // Monthly total cost

        // Get the loan amount.
        loan = decimal.Parse(loanTextBox.Text);
        // Get the insurance amount.
        insurance = decimal.Parse(insTextBox.Text);
        // Get the gas amount.
        gas = decimal.Parse(gasTextBox.Text);
        // Get the oil amount.
        oil = decimal.Parse(oilTextBox.Text);
        // get the tires amount.
        tires = decimal.Parse(tiresTextBox.Text);
        // Get the maintenance amount.
        maintenance = decimal.Parse(mainTextBox.Text);
        // determine the monthly cost.


        // Calculate monthly cost.
        monthlyCost = loan + insurance + gas + oil + tires + maintenance;
        totalMonthlyLabel.Text = monthlyCost.ToString();

You have to get different type of monthly expenses from your user (that will be your input). Then your program is supposed to perform some calculations on that input in order to get some output (ie monthlyCost in your case). You don't have to get monthlyCost from your user.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM