C# 为什么我的输出等于零?(学生)

C# 为什么我的输出等于零?(学生),c#,C#,我是CIS课程第一年的学生,所以我是初学者。我们应该从一个文本文件中获取输入,其中包含一名员工、该员工所在的部门、每小时工资和给定周的工作时间,如下所示: EID001, 1, 10.00, 40 EID002, 2, 10.50, 35 EID003, 3, 11.00, 30 EID004, 4, 11.50, 25 EID005, 5, 12.00, 20 EID006, 6, 12.50, 40 EID007, 7, 11.00, 25 . . . (Employee ID,

我是CIS课程第一年的学生,所以我是初学者。我们应该从一个文本文件中获取输入,其中包含一名员工、该员工所在的部门、每小时工资和给定周的工作时间,如下所示:

EID001, 1, 10.00, 40 
EID002, 2, 10.50, 35 
EID003, 3, 11.00, 30 
EID004, 4, 11.50, 25 
EID005, 5, 12.00, 20 
EID006, 6, 12.50, 40 
EID007, 7, 11.00, 25
.
.
.
(Employee ID, Department, Hourly Salary, Hours Worked)
然后将每个字段分隔为数组元素,验证每个字段,并计算7个部门的总工资(小时工资*工作小时数)

我的代码:

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Project_3_rev
{
    class Program
    {
        const int DEPARTMENTS = 7;
        const int FIELDS = 4;
        static void Main(string[] args)
        {
            FileStream fStream = new FileStream("project3data.txt", FileMode.Open, FileAccess.Read);
            StreamReader inFile = new StreamReader(fStream);
            string input = "";
            string[] fields = new string[FIELDS];
            string employee = "";
            int department = 0;
            double salary = 0.00;
            int hours = 0;


            double totalSalary = 0.00;
            int totalHours = 0;
            double[] grossPayArray = new double[DEPARTMENTS];


            input = inFile.ReadLine();
            while(input != null)
            {
                fields = input.Split(',');

                checkEmployee(input, fields, employee);
                checkDepartment(input, fields, department);

                for(int x = 1; x <= DEPARTMENTS; x++)
                {
                    totalSalary = 0.00;
                    totalHours = 0;
                    while (department == x)
                    {

                        checkSalary(input, fields, salary, department, totalSalary);
                        checkHours(input, fields, hours, department, totalHours);
                        grossPayArray[x - 1] = totalSalary * totalHours;
                    }
                }

                input = inFile.ReadLine();
            }


            displayOutput(grossPayArray);

        }

        static void checkEmployee(string inp, string[] fieldsArray, string emp)
        {
            if(fieldsArray[0] == null)
                Console.WriteLine("An Employee ID is invalid.");
        }

        static void checkDepartment(string inp, string[] fieldsArray, int dept)
        {
            if((!int.TryParse(fieldsArray[1], out dept)) || dept < 0 || dept > DEPARTMENTS)
            {
                Console.WriteLine("Department field is invalid: " + fieldsArray[1]);
            }

        }

        static void checkSalary(string inp, string[] fieldsArray, double sal, int dept, double totSal)
        {

                    if ((double.TryParse(fieldsArray[2], out sal)) && sal >= 10.00)
                        totSal = totSal * sal;
                    else
                        Console.WriteLine("Salary field is invalid: " + fieldsArray[2]);



        }

        static void checkHours(string inp, string[] fieldsArray, int hrs, int dept, int totHrs)
        {

                    if ((int.TryParse(fieldsArray[3], out hrs)) && hrs >= 0)
                        totHrs = totHrs * hrs;

                    else
                        Console.WriteLine("Hours field is invalid: " +  fieldsArray[3]);



        }


        static void displayOutput(double[] grossArray)
        {
            for(int x = 1; x <= DEPARTMENTS; x++)
            {
                Console.WriteLine("Department " + x + ": Gross Pay = " + grossArray[x-1]);
            }
        }






    }
}
为什么总工资等于零


请记住,我是计算机科学的第一年。我只允许使用我们所学的知识,我只知道基本知识。如果这个代码很混乱或者我的逻辑有点偏离,请让我放松一下。提前谢谢

在这两行代码中:

totalSalary = 0.00;
totalHours = 0;
将值设置为零。将它们乘以以后的任何值,仍然是零。您需要确保在相乘之前使用正确的值更新这些字段,或者至少将其设置为1。

问题在于:

for(int x = 1; x <= DEPARTMENTS; x++)
{
    totalSalary = 0.00;
    totalHours = 0;
    while (department == x)
    {
        checkSalary(input, fields, salary, department, totalSalary);
        checkHours(input, fields, hours, department, totalHours);
        grossPayArray[x - 1] = totalSalary * totalHours;
    }
}
这样,函数实际上只验证工资,正如它被命名的那样。参见:单一责任原则

然后在循环本身内设置您的总工资:

    while (department == x)
    {
        checkSalary(input, fields, salary, department, totalSalary);
        checkHours(input, fields, hours, department, totalHours);

        totalSalary *= salary;
        totalHours += hours;

        grossPayArray[x - 1] = totalSalary * totalHours;
    }

编辑:当然,这仍然存在一个问题,即
工资在循环中不会发生变化。

您的代码的问题是这种函数:

static void checkSalary(string inp, string[] fieldsArray, double sal, int dept, double totSal)
{
    if ((double.TryParse(fieldsArray[2], out sal)) && sal >= 10.00)
        totSal = totSal * sal;
    else
        Console.WriteLine("Salary field is invalid: " + fieldsArray[2]);
}
function Increase(int number) {
    number = number + 1;
}

var number = 5;
Increase(number);
Console.WriteLine(number);
类似于这种函数:

static void checkSalary(string inp, string[] fieldsArray, double sal, int dept, double totSal)
{
    if ((double.TryParse(fieldsArray[2], out sal)) && sal >= 10.00)
        totSal = totSal * sal;
    else
        Console.WriteLine("Salary field is invalid: " + fieldsArray[2]);
}
function Increase(int number) {
    number = number + 1;
}

var number = 5;
Increase(number);
Console.WriteLine(number);
你得到的是5,而不是你的期望,6

在C#中,它被称为“按值传递”。该值被传递到函数中,对外部变量的修改不受影响。在您的情况下,toSalary将保持为0

您需要更改为
ref double to salary
以使用“参照传递”

您还需要练习调试技术:

  • 放断点

  • 打印要检查的值

  • 编写单元测试来验证您的方法


看起来您将“grossPayArray[x-1]=totalSalary*totalHours;”正确,但您似乎没有从初始值0更新totalSalary或totalHours

您需要读取该文件。 拆分这些值。 从零开始更新变量。 执行您的函数,在本例中为乘法


希望对您有所帮助。

在StackExchange的代码审阅部分,此类问题可能会更好

尽管如此,下面是一个使用类和LINQ的示例解决方案,供您在今年晚些时候参考:

要运行:

var app=new app_DepartmentPay();
app.Run()

输出:


你能解释一下为什么你认为该网站的未来访问者会发现这个问题得到了很好的研究,并且很清楚,最重要的是很容易识别,因为他们有相同的问题吗?我按照你的建议做了,我仍然得到了零分。我是否必须对这些行执行不同的操作:totalSalary=0.00;总小时数=0;我尝试过删除它们并更改它们的值,结果仍然是零。我猜
工资
小时数
也是零。你也不会在循环中改变它们。正如我提到的,您可能需要完全重写
validateSalary
函数。另外,
out sal
位没有意义。
using System;
using System.IO;
using System.Linq;

namespace StackOverflow
{
    public class App_DepartmentPay
    {
        public void Run()
        {
            var lines = File.ReadAllLines(@"d:\temp\payments.txt");

            var payStubs = lines.Select(l => new PayStub(l)).ToList();

            var departmentTotals = payStubs.GroupBy(p => p.Department).Select(g => new DepartmentTotal(g.Key, g.Sum(w => w.Pay))).ToList();

            departmentTotals.ForEach(t => Console.WriteLine(t.ToString()));
        }
    }

    public class PayStub
    {
        public string EmployeeId { get; private set; }

        public int Department { get; private set; }

        public decimal HourlyWage { get; private set; }

        public decimal HoursWorked { get; private set; }

        public decimal Pay
        {
            get
            {
                return HourlyWage * HoursWorked;
            }
        }

        public PayStub(string payLine)
        {
            var contents = payLine.Split(',');

            EmployeeId = contents[0];

            Department = int.Parse(contents[1]);

            HourlyWage = decimal.Parse(contents[2]);

            HoursWorked = decimal.Parse(contents[3]);
        }
    }

    public class DepartmentTotal
    {
        public int Department { get; set; }

        public decimal TotalPay { get;  set; }

        public DepartmentTotal(int department, decimal pay)
        {
            Department = department;
            TotalPay = pay;
        }

        public override string ToString()
        {
            return $"Department: {Department} \tGross Pay: {TotalPay}";
        }
    }
}