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

时间:2017-12-06 03:02:50

标签: 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, 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]);
            }
        }






    }
}

我的输出:

Department 1: Gross Pay = 0
Department 2: Gross Pay = 0
Department 3: Gross Pay = 0
Department 4: Gross Pay = 0
Department 5: Gross Pay = 0
Department 6: Gross Pay = 0
Department 7: Gross Pay = 0

为什么总收入等于零?

请记住,我是计算机科学的第一年。我只被允许使用我们学到的东西,我只知道基础知识。如果这段代码很乱或我的逻辑已经消失,请给我一点松懈。提前谢谢。

5 个答案:

答案 0 :(得分:0)

在这两行代码中:

totalSalary = 0.00;
totalHours = 0;

您将值设置为零。将它们乘以后,它们仍将为零。您需要确保在乘法之前使用正确的值更新这些字段,或者至少将它们设置为1。

答案 1 :(得分:0)

问题在于:

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;
    }
}

请注意,您永远不会更改变量totalSalarytotalHours

我建议你做的是重新定义你的功能,只验证输入工资:

static void validateSalary(string inp, string[] fieldsArray, double sal, int dept, double totSal)
{
    // you might want to rewrite this
    if !((double.TryParse(fieldsArray[2], out sal)) && sal >= 10.00)
        Console.WriteLine("Salary field is invalid: " + fieldsArray[2]);
}

这样,该函数实际上只有验证工资,因为它已被命名。请参阅:单一责任原则

然后在循环中设置总薪水:

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

        totalSalary *= salary;
        totalHours += hours;

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

编辑:当然,这仍然存在salary在循环中不会发生变化的问题。

答案 2 :(得分:0)

您的代码问题就是这种功能:

pthreads

类似于这种功能:

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]);
}

你得到的是5,而不是你的期望,6。

在C#中,它被称为&#34;通过值&#34;传递。该值将传递给函数,并且不会对外部变量进行任何修改。在您的情况下,toSalary将保持为0。

您需要更改为function Increase(int number) { number = number + 1; } var number = 5; Increase(number); Console.WriteLine(number); 以使用&#34;通过引用传递&#34;。

您需要练习的另一件事是调试技术:

  • 提出断点。

  • 打印要检查的值。

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

答案 3 :(得分:0)

看起来你正在成倍增加&#39; grossPayArray [x - 1] = totalSalary * totalHours;&#39;正确,但似乎你没有从初始值0更新totalSalary或totalHours。

您需要阅读该文件。  拆分值。   从零更新变量。    执行您的功能,在这种情况下,乘以。

希望有所帮助

答案 4 :(得分:0)

在StackExchange的Code Review部分,这类问题可能会更好。

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

运行:

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

输出:

enter image description here

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}";
        }
    }
}