簡體   English   中英

C# 獎金計算器

[英]C# Bonus Calculator

創建一個 Bonus Calculator 程序,它有兩個重載方法——一個接受以雙精度表示的工資和獎金,另一個接受以雙精度表示的工資和整數形式的獎金。

我寫了這個程序,我可以得到作為 int 的獎金來工作,但它們都作為 double 不起作用

namespace BonusCalculation
{
 class Bonus
 {
   static void Main(string[] args)
   { 
        double salary;
        int bonus;
        double bonusPercent;

        WriteLine("What is your salary?");
        salary = Convert.ToDouble(ReadLine());
        WriteLine("What is your bonus?");
        string bonusString = Console.ReadLine();
        if (int.TryParse(bonusString, out bonus))
        { CalcBonus(salary, bonus); }
        else if((double.TryParse(bonusString, out bonusPercent)))
        { CalcBonus(salary, bonusPercent); }

        WriteLine( "Your new salary is {0:c2}", CalcBonus(salary,bonus));
    }
  static double CalcBonus(double s,double b)
    {
        s = (s * b) + s;
        return s;
    }
 static double CalcBonus(double s, int b)
    {
        s = s + b;
        return s;
    }
  }
}

當我以雙倍獎勵運行該程序時,它不會進行數學運算。 感謝您的任何幫助。

問題在這里:

    if (int.TryParse(bonusString, out bonus))
    { CalcBonus(salary, bonus); }
    else if((double.TryParse(bonusString, out bonusPercent)))
    { CalcBonus(salary, bonusPercent); }

    WriteLine( "Your new salary is {0:c2}", CalcBonus(salary,bonus));

如果bonusString不是有效整數,則永遠不會設置bonus ,並且WriteLineCalcBonus的最后一次調用使用0作為bonus 值。

與其嘗試推斷獎金類型,不如讓用戶指定他們輸入的是百分比還是值,並且只做一次數學運算而不是再次執行WriteLine調用。

 public partial class Form1 : Form
{
    // Constant field for the contribution rate
    private const decimal CONTRIB_RATE = 0.05m;

    public Form1()
    {
        InitializeComponent();
    }

    // The InputIsValid method converts the user input and stores
    // it in the arguments (passed by reference). If the conversion
    // is successful, the method returns true. Otherwise it returns
    // false.
    private bool InputIsValid(ref decimal pay, ref decimal bonus)
    {
        // Flag variable to indicate whether the input is good
        bool inputGood = false;

        // Try to convert both inputs to decimal.
        if (decimal.TryParse(grossPayTextBox.Text, out pay))
        {
            if (decimal.TryParse(bonusTextBox.Text, out bonus))
            {
                // Both inputs are good.
                inputGood = true;
            }
            else
            {
                // Display an error message for the bonus.
                MessageBox.Show("Bonus amount is invalid.");
            }
        }
        else
        {
            // Display an error message for gross pay.
            MessageBox.Show("Gross pay is invalid.");
        }

        // Return the result.
        return inputGood;
    }

    private void calculateButton_Click(object sender, EventArgs e)
    {
        // Variables for gross pay, bonus, and contributions
        decimal grossPay = 0m, bonus = 0m, contributions = 0m;

        if (InputIsValid(ref grossPay, ref bonus))
        {
            // Calculate the amount of contribution.
            contributions = (grossPay + bonus) * CONTRIB_RATE;

            // Display the contribution.
            contributionLabel.Text = contributions.ToString("c");
        }
    }

    private void exitButton_Click(object sender, EventArgs e)
    {
        // Close the form.
        this.Close();
    }
}

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM