C# 添加用户定义的数据

C# 添加用户定义的数据,c#,C#,我想说的第一件事是这是我的家庭作业。我不是在寻找答案,只是在找出我做错了什么方面寻求帮助。这是我第一次上编程课,到目前为止,我一直做得很好。现在我突然迷路了 问题是: 创建一个应用程序,允许用户输入因操作其汽车而产生的以下费用的每月成本:贷款付款、保险、汽油、机油、轮胎和维护。然后,程序应显示这些费用的月度总成本和年度总成本 这就是我建造的: private void totalMonthlyButton_Click(对象发送者,事件参数e) { decimal loan;

我想说的第一件事是这是我的家庭作业。我不是在寻找答案,只是在找出我做错了什么方面寻求帮助。这是我第一次上编程课,到目前为止,我一直做得很好。现在我突然迷路了

问题是: 创建一个应用程序,允许用户输入因操作其汽车而产生的以下费用的每月成本:贷款付款、保险、汽油、机油、轮胎和维护。然后,程序应显示这些费用的月度总成本和年度总成本

这就是我建造的:

private void totalMonthlyButton_Click(对象发送者,事件参数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.
在此方面的任何帮助都是非常感谢的。同样,我不想得到答案,只是想知道我是否朝着正确的方向前进,如果我完全离开了,下一步该去哪里等等


谢谢大家。

您必须从您的用户那里获得不同类型的每月费用(这将是您的输入)。然后您的程序应该对该输入执行一些计算,以便获得一些输出(例如,您的情况下,
monthlyCost
)。您不必从用户那里获取
每月成本

试试这个,我删除了一个变量,因为该变量不读取值,它只需显示值即可

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

这是我现在能想到的最好的方法:

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 { }

我能给初学者的最好建议是减少你写评论的数量。你的vars\methods应该足够清楚。你的问题在哪里?你想把每月总成本插入一个文本框吗?不应该解析monthlyCost,因为这是你最终想要的变量。一旦你这样做了,就显示结果(monthlyCost)在消息框中。不要忘记使用.ToString()方法。您的代码看起来很好。如果要将
monthlyCost
十进制变量转换为
String
,请使用
monthlyCost.ToString()
并将其插入正确的文本框是的,我构建它的方式是用户将这些信息输入文本框,然后答案将填充到标签中。到目前为止,我没有显示任何错误,但是当我试图告诉程序在何处显示月成本时,我在其中输入的所有内容都会导致错误。是吗您包括如何“尝试告诉程序在何处显示”我没有看到那一部分。请放心,你们摇滚吧!谢谢你们,我忘记的只是ToString部分。我怎么错过了这一部分,我不知道。我已经完成了这个项目,包括我自己能够完成的年度部分。感谢所有帮助过这个项目的人,因为我过去一直在强调这一点好几天了。