2015-11-10 47 views
0

我一直在研究一个我想做一个简单计算的问题。我对C#很陌生,但我试图学习。C#,在表单中实现单选按钮来计算Nest Egg

基本上,如果给出四种不同的输入,我必须节省多少资金:收入需求,税率,预计回报率和每个时间段的收入。

我正在使用一组单选按钮来选择每个时间段的收入,基本上是每日,每周,每月,每年的收入。因此,单选按钮选择收入如何列为价值,但我不确定如何将此部分的变量存入计算部分。我将提供一个代码片段。

任何帮助将不胜感激,谢谢。

private void radioButton2_CheckedChanged(object sender, EventArgs e) 
    { 
     // Local variables 
     decimal income; 
     // Get the income, tax rate and rate of return. 
     income = decimal.Parse(incomeDesiredTextBox.Text); 

     // Determine pay period 
     if (true) 
     { 
      if (incomePerDayRadioButton.Checked) 
      { 
       decimal newIncome = income/365; 
       return; 
      } 
      else if (incomePerMonthRadioButton.Checked) 
      { 
       decimal newIncome = income/24; 
       return; 
      } 
      else if (incomePerWeekRadioButton.Checked) 
      { 
       decimal newIncome = income/52; 
       return; 
      } 
      else if (incomePerYearRadioButton.Checked) 
      { 
       decimal newIncome = income/1; 
       return; 
      } 
     } 
    } 

    private void calculateButton_Click(object sender, EventArgs e) 
    { 
     // Local variables 
     decimal income; 
     decimal newIncome; 
     decimal taxRate; 
     decimal rateOfReturn; 

     // Get the income, tax rate and rate of return. 
     income = decimal.Parse(incomeDesiredTextBox.Text); 
     taxRate = decimal.Parse(totalTaxesTextBox.Text); 
     rateOfReturn = decimal.Parse(rateOfReturnTextBox.Text); 


     //change rate of return to decimal format 
     var rateOfReturnPercentage = rateOfReturn/100; 

     // Calculate Nest Egg. 
     decimal taxedIncome = newIncome * taxRate; 



    } 
+0

![有效的XHTML(https://gyazo.com/b52272b73c9915f2c612793bf62f87fd)。 – Crawlersout

+0

我已经包含了一个到目前为止的形式看起来像Gyazo的链接。 – Crawlersout

回答

0

您应该创建一个计算新收入的独立方法。

public decimal CalculateNewIncome() 
{ 
     decimal income; 
     // Get the income, tax rate and rate of return. 
     income = decimal.Parse(incomeDesiredTextBox.Text); 

     if (incomePerDayRadioButton.Checked) 
     { 
      return income/365; 
     } 
     else if (incomePerMonthRadioButton.Checked) 
     { 
      return income/24; // should be 30 ? 
     } 
     else if (incomePerWeekRadioButton.Checked) 
     { 
      return income/52; 
     } 
     else if (incomePerYearRadioButton.Checked) 
     { 
      return income; 
     } 
} 

,然后你可以做计算为

private void calculateButton_Click(object sender, EventArgs e) 
{ 
    // Local variables 
    decimal income; 
    decimal newIncome = CalculateNewIncome(); 
    decimal taxRate; 
    decimal rateOfReturn; 

    // Get the income, tax rate and rate of return. 
    income = decimal.Parse(incomeDesiredTextBox.Text); 
    taxRate = decimal.Parse(totalTaxesTextBox.Text); 
    rateOfReturn = decimal.Parse(rateOfReturnTextBox.Text); 


    //change rate of return to decimal format 
    var rateOfReturnPercentage = rateOfReturn/100; 

    // Calculate Nest Egg. 
    decimal taxedIncome = newIncome * taxRate; 

    // display it 
} 
+0

谢谢你的帮助,这比我想要做的更有意义。你会推荐哪些资源来更好地了解这种语言? – Crawlersout

+0

@Crawlersout - 继续努力,只需要很短的时间,不断练习。 –