我的C#代码中的逻辑错误,我该怎么办?

时间:2008-11-14 10:38:27

标签: c#

我有一个逻辑错误。我提供了以下输入:

  • 薪水是30000
  • 孩子n°是9

所以净工资将是:

  • 家庭奖金+工资 - 税收

     (750)       + (30000) - (3000)
    
  • 但我的程序将它们视为

      (1500)    + (30000) + (6000)
    

我的计划加倍(累积)家庭奖金和税金。任何人都可以解释原因吗?

class Program
{
    static void Main(string[] args)
    {
        Employee e = new Employee();
        e.ReadEmployee();
        e.PrintEmployee();
    }
}

class Employee
{
    private string n;
    private int byear;
    private double sal;
    private bool gen;
    private bool mar;
    private int child;
    public static double tax = 0;
    public static double familybonus = 0;
    public string Ename
    {
        get { return this.n; }
        set
        {
            this.n = value;

        }
    }
    public int Birthyear
    {
        get { return this.byear; }
        set
        {
            if (value >= 1970 && value <= 1990) this.byear = value;
            else this.byear = 0;
        }
    }
    public double Salary
    {
        get { return this.sal; }
        set
        {
            if (value >= 5000 && value <= 50000) this.sal = value;
            else this.sal = 0;
        }
    }

    public bool Gender
    {
        get { return this.gen; }
        set { this.gen = value; }
    }
    public bool Married
    {
        get { return this.mar; }
        set { this.mar = value; }
    }
    public int NChildren
    {
        get { return this.child; }
        set
        {
            if (value >= 0 && value <= 12) this.child = value;
            else this.child = 0;
        }
    }

    public double getAge()
    {
        return 2008 - this.Birthyear;
    }
    public double getNet()
    {
        double net = getFamilyBonus() + this.Salary - getTax();
        return net;
    }

    public double getFamilyBonus()
    {

        if (this.Married == true)
            familybonus += 300;
        if (this.NChildren == 1) familybonus += 200;
        else if (this.NChildren == 2) familybonus += 350;
        else if (this.NChildren >= 3) familybonus += 450;
        return familybonus;
    }

    public double getTax()
    {
        if (Salary < 10000)
            tax = 0;
        if (Salary <= 10000 && Salary >= 20000)
            tax += Salary * 0.05;
        else tax += Salary * 0.1;
        return tax;

    }

    public void ReadEmployee()
    {
        Console.Write("Enter Employee Name: ");
        Ename = Console.ReadLine();
        Console.Write("Enter Employee birth date: ");
        Birthyear = int.Parse(Console.ReadLine());

        while (Birthyear < 1970 || Birthyear > 1990)
        {
            Console.WriteLine("Invalid Birthyear!");
            Console.Write("Enter Employee Birth date: ");

            Birthyear = int.Parse(Console.ReadLine());
        }
        string g = null;
        while (g != "M" && g != "m" && g != "F" && g != "f")
        {
            Console.Write("Enter Employee Gender (M/F)");
            g = Convert.ToString(Console.ReadLine());
        }

        if (g == "M" || g == "m")
            Gender = true;
        else
            Gender = false;
        Console.Write("Enter Employee Salary: ");
        Salary = Double.Parse(Console.ReadLine());
        while (Salary < 5000 || Salary > 50000)
        {
            Console.WriteLine("Invalid Salary!");
            Console.Write("Enter Employee Salary: ");
            Salary = int.Parse(Console.ReadLine());
        }
        string m = null;
        while (m != "true" && m != "True" && m != "false" && m != "False")
        {
            Console.Write("Married (true/false)");
            m = Console.ReadLine();

        }
        if (m == "true")
            this.Married = true;
        else
            this.Married = false;

        Console.Write("Enter Employee Children count: ");
        NChildren = int.Parse(Console.ReadLine());

        while (NChildren < 0 || NChildren > 12)
        {
            Console.WriteLine("Invalid NChildren!");
            Console.Write("Enter Employee Children count: ");

            NChildren = int.Parse(Console.ReadLine());
        }

    }
    public void PrintEmployee()
    {
        Console.Write("Hello ");
        {
            if (Gender == true)
                Console.Write("Mr. ");
            else
                Console.Write("Mrs. ");
            Console.WriteLine(Ename);
        }
        Console.WriteLine("You are {0} years old", getAge());
        Console.WriteLine("Salary= {0}", Salary);
        Console.WriteLine("Tax= {0}", getTax());
        Console.WriteLine("Family bonus= {0}", getFamilyBonus());
        Console.WriteLine("Net= {0}", getNet());
    }
}

2 个答案:

答案 0 :(得分:2)

我接受了现有代码,并硬连接了输入(而不是使用Console.ReadLine()),我得到:

  

你28岁,薪水= 30000   税= 3000家庭奖金= 750净= 25500

主要问题似乎不是初始化值 - 即将字段视为变量:

public double getTax()
{
    if (Salary < 10000)
        tax = 0;
    if (Salary <= 10000 && Salary >= 20000)
        tax += Salary * 0.05;
    else tax += Salary * 0.1;
    return tax;
}

好的 - taxSalary >= 10000开始的内容是什么,等等familyBouns getFamilyBonus。顺便说一句,薪水如何同时为<= 10000>= 20000

为了说明,我已将输出更改为:

    Console.WriteLine("Tax= {0}", getTax());
    Console.WriteLine("Tax= {0}", getTax());
    Console.WriteLine("Tax= {0}", getTax());

显示:

  

税= 3000税= 6000税= 9000

我的建议是:不要存储计算值,除非你知道数学是如此复杂以至于值得。只需根据需要计算它们(根本没有字段)。

答案 1 :(得分:0)

另一个问题似乎在于当你说familybonus + = 300时你没有初始化familybonus。所以每次你调用GetFamilybonus它都会被添加到之前的结果中。你可以在PrintEmployee函数中调用它两次,一次直接调用getNet;