C# 奖金计算器

C# 奖金计算器,c#,C#,创建一个奖金计算器程序,该程序有两个重载方法——一个接受工资和奖金都表示为double,另一个接受工资作为double,奖金作为int 我写的程序,我可以得到奖金作为一个整数工作,但他们都作为双倍将不起作用 namespace BonusCalculation { class Bonus { static void Main(string[] args) { double salary; int bonus; double bon

创建一个奖金计算器程序,该程序有两个重载方法——一个接受工资和奖金都表示为double,另一个接受工资作为double,奖金作为int

我写的程序,我可以得到奖金作为一个整数工作,但他们都作为双倍将不起作用

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));
如果BonuString不是有效的整数,则永远不会设置奖金,并且在WriteLine中对CalcBonus的最后一次调用使用0作为奖金值

与其尝试推断奖金类型,不如让用户指定他们输入的是百分比还是值,并且只计算一次,而不是在WriteLine调用中再次计算


}

你说它不算数是什么意思?您提供了什么输入,得到了什么输出?此外,我会让用户指定输入是值还是百分比,而不是在解析时尝试推断它。20是一个有效的整数和一个有效的双精度。你的程序非常混乱。您有两个对CalcBonus的调用,它们对返回值不做任何处理,然后您有另一个对它的调用,即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();
    }
}