【问题标题】:Adding User Defined data添加用户定义的数据
【发布时间】:2015-02-07 16:54:03
【问题描述】:

首先我想说的是,这是我的作业。我不是在寻找答案,只是帮助找出我做错了什么。这是我的第一个编程课,到目前为止我一直做得很好。现在突然间我迷路了。

问题: 创建一个应用程序,让用户输入运营他或她的汽车所产生的以下费用的每月成本:贷款支付、保险、汽油、机油、轮胎和维护。然后程序应显示这些费用的每月总成本和这些费用的年度总成本。

这是我构建的:

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.

非常感谢您对此的任何帮助。再说一次,我不希望只是一些提示,如果我朝着正确的方向前进,如果我完全离开,下一步要去哪里等等。

谢谢大家。

【问题讨论】:

  • 我能给初学者的最佳建议是减少您编写的 cmets 的数量。您的 vars\methods 应该足够清楚。你的问题在哪里?您想将每月总费用插入文本框吗?
  • monthlyCost 不应该被解析,因为这是你最终想要的变量。完成添加后,只需在 MessageBox 左右显示结果(monthlyCost)。不要忘记使用 .ToString() 方法。
  • 您的代码看起来不错。如果要将monthlyCost十进制变量转换为String,请使用monthlyCost.ToString()并将其插入正确的文本框
  • 是的,我构建它的方式是用户将在那里输入信息到文本框中,然后答案将填充到标签中。到目前为止,我没有显示任何错误,但是当我尝试告诉程序在哪里显示monthlyCost 时,我放在那里的所有内容都会导致错误。
  • 你是否包括了你如何“试图告诉程序在哪里显示”?我没有看到那部分。随意edit那在

标签: c#


【解决方案1】:

这是我目前能想到的最好方法:

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 你们摇滚!谢谢你,我忘记的只是 ToString 部分。我是如何错过的,我不知道。我已经完成了这个项目,包括我能够自己完成的年度部分。感谢所有为此提供帮助的人,因为过去几天我一直在强调这一点。
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 2017-03-06
      • 2011-02-17
      • 2023-03-29
      • 1970-01-01
      • 2016-08-15
      • 2015-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多