2015-11-10 48 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 - 繼續努力,只需要很短的時間,不斷練習。 –