fileIn,fileOut错误 - 我无法弄清楚

时间:2016-11-30 18:12:02

标签: c# file

我已经为此工作了两个星期了,我知道我错过了一些小事。我必须将旧项目修改为新项目。第一个项目是,如果有人进入他们的工作类型(例如:w为工人,s为主管,m为经理等),他们的年龄和经验。该计划将告诉他们是否可以退休。如果他们不能退休,它告诉他们他们需要修复什么才能退休。

新程序必须使用fileIn并生成一个fileOut,显示工作者ID,年龄,经验和资格。毕竟显示出他们想要的平均年龄和经验。

我的老师是模块化标题和主要在顶部的坚持者。我在下面的代码中输入了我的2015工作室,最后收到三条错误消息。 fileIn也在下面。 Util是我老师的参考,我不知道如何复制它。

有人可以给我任何建议吗?我完全是绿色的,并且不熟悉编码所以我还没有完成所有条款,所以我提前道歉。

using System;
using System.IO;
using System.Text.RegularExpressions;
using Util;

namespace Program_9
{
class Program
{
    const string INPUT_FILE_NAME = "C:\\Users\\Programing 1\\Soluation 1\\Program 9\\Retirement Numbers\\Input";
    const string OUTPUT_FILE_NAME = "C:\\Users\\Programing 1\\Soluation 1\\Program 9\\Retirement Numbers\\Output";

    static string lineIn;
    static StreamReader fileIn;
    static StreamWriter fileOut;
    static char WType, WElig;
    static uint id;
    static double WAge, WExp;
    static double numOfEmployees;
    static double AvgAge, AvgExp, AgeAvgTotal = 0.0, WAge1Total = 0.0, WAge2Total = 0.0, WAge3Total = 0.0, WAge4Total = 0.0, WAge5Total = 0.0;
    static double WAge6Total = 0.0, WAge7Total = 0.0, WAge8Total = 0.0, WAge9Total = 0.0, WAge10Total = 0.0, WAge11Total = 0.0, WAge12Total = 0.0;
    static double ExpAvgTotal = 0.0, WExp1Total = 0.0, WExp2Total = 0.0, WExp3Total = 0.0, WExp4Total = 0.0, WExp5Total = 0.0;
    static double WExp6Total = 0.0, WExp7Total = 0.0, WExp8Total = 0.0, WExp9Total = 0.0, WExp10Total = 0.0, WExp11Total = 0.0, WExp12Total = 0.0;

    static void Main()
    {
        OpenFiles();
        PrintReportHeadings();
        while ((lineIn = fileIn.ReadLine()) != null)
        {
            ParseLineIn();
            PrintDetailLine();
            UpdateTotals();
        }
        CalcAvg();
        PrintAvg();
        CloseFiles();
        DspData();
    }

    static void OpenFiles()
    {
        if (File.Exists(INPUT_FILE_NAME))
        {
            fileIn = File.OpenText(INPUT_FILE_NAME);
            Console.WriteLine("{0} was opened", INPUT_FILE_NAME);
        }
        else
        {
            Console.WriteLine("Error: {0} does not exit\n", INPUT_FILE_NAME);
            ConIO.Exit();
        }
        if (File.Exists(OUTPUT_FILE_NAME))
        {
            fileOut = File.CreateText(OUTPUT_FILE_NAME);
            Console.WriteLine("{0} was created\n", OUTPUT_FILE_NAME);
        }
        else
        {
            Console.WriteLine("Error: {0} could not be created\n", OUTPUT_FILE_NAME);
            ConIO.Exit();
        }
    }

    static void PrintReportHeadings()
    {
        fileOut.WriteLine("Employee  Age  Experience  Eligibility");
        fileOut.WriteLine("{0,9},  {1,7},  {2,7}, {3}", id, WAge, WExp, WElig);
        fileOut.WriteLine();
        fileOut.WriteLine("Average {0,7:}, {1,7}", AvgAge, AvgExp);
    }
    static void ParseLineIn()
    {
        string[] words = new string[4];

        lineIn = lineIn.Trim();
        while (Regex.IsMatch(lineIn, "[ ]{2}"))
            lineIn = lineIn.Replace("  ", " ");
        words = lineIn.Split(' ');
        id = UInt32.Parse(words[0]);
        WType = char.Parse(words[1]);
        WAge = int.Parse(words[2]);
        WExp = int.Parse(words[3]);
    }

    static void PrintDetailLine()
    {
        fileOut.WriteLine("{0} {1,7:d} {2,7:d} {3,7:d} {4,7:d} {5,3}",
            id, WAge, WExp, WElig);
    }

    static void UpdateTotals(int WAge1, double WAge2, double WAge3, double WAge4, double WAge5, double WAge6, double WAge7, double WAge8, double WAge9, double WAge10, double WAge11, double WAge12, double WExp1, double WExp2, double WExp3, double WExp4, double WExp5, double WExp6, double WExp7, double WExp8, double WExp9, double WExp10, double WExp11, double WExp12)
    {
        numOfEmployees++;
        WAge1Total += WAge1;
        WAge2Total += WAge2;
        WAge3Total += WAge3;
        WAge4Total += WAge4;
        WAge5Total += WAge5;
        WAge6Total += WAge6;
        WAge7Total += WAge7;
        WAge8Total += WAge8;
        WAge9Total += WAge9;
        WAge10Total += WAge10;
        WAge11Total += WAge11;
        WAge12Total += WAge12;
        AgeAvgTotal += AvgAge;
        WExp1Total += WExp1;
        WExp2Total += WExp2;
        WExp3Total += WExp3;
        WExp4Total += WExp4;
        WExp5Total += WExp5;
        WExp6Total += WExp6;
        WExp7Total += WExp7;
        WExp8Total += WExp8;
        WExp9Total += WExp9;
        WExp10Total += WExp10;
        WExp11Total += WExp11;
        WExp12Total += WExp12;
        ExpAvgTotal += AvgExp;
    }

    static void CalcAvg(int WAge1, double WAge2, double WAge3, double WAge4, double WAge5, double WAge6, double WAge7, double WAge8, double WAge9, double WAge10, double WAge11, double WAge12)
    {
        AgeAvgTotal = (WAge1 + WAge2 + WAge3 + WAge4 + WAge5 + WAge6 + WAge7 + WAge8 + WAge9 + WAge10 + WAge11 + WAge12);
        AvgAge = AgeAvgTotal / numOfEmployees;
        AvgExp = ExpAvgTotal / numOfEmployees;
    }

    static void PrintAvg()
    {
        fileOut.WriteLine(" ");
        fileOut.WriteLine("Avg {0,7:f} {1,7:f}", AvgAge, AvgExp);
    }

    static void CloseFiles()
    {
        fileIn.Close(); fileOut.Close();
    }

    static void DspData()
    {
        switch (WType=WElig)
        {
            case 'W':
            case 'w':
                if (WAge >= 63 && WExp >= 25)
                    Console.WriteLine("{0} {1} {2} Employee can retire.", id, WAge, WExp);
                else if (WAge >= 63 && WExp < 25)
                    Console.WriteLine("{0} {1} {2} Employee cannot retire due to lack of experience.", id, WAge, WExp);
                else if (WAge < 63 && WExp >= 25)
                    Console.WriteLine("{0} {1} {2} Employee cannot retire due to being underage.", id, WAge, WExp);
                else if (WAge < 63 && WExp < 25)
                    Console.WriteLine("{0} {1} {2} Employee cannot retire due to being underage and lack of experience.", id, WAge, WExp);
                break;
            case 'S':
            case 's':
                if (WAge >= 60 && WExp >= 24)
                    Console.WriteLine("{0} {1} {2} Employee can retire.", id, WAge, WExp);
                else if (WAge >= 60 && WExp < 24)
                    Console.WriteLine("{0} {1} {2} Employee cannot retire due to lack of experience.", id, WAge, WExp);
                else if (WAge < 60 && WExp >= 24)
                    Console.WriteLine("{0} {1} {2} Employee cannot retire due to being underage.", id, WAge, WExp);
                else if (WAge < 60 && WExp < 24)
                    Console.WriteLine("{0} {1} {2} Employee cannot retire due to being underage and lack of experience.", id, WAge, WExp);
                break;
            case 'M':
            case 'm':
                if (WAge >= 55 && WExp >= 20)
                    Console.WriteLine("{0} {1} {2} Employee can retire.", id, WAge, WExp);
                else if (WAge >= 55 && WExp < 20)
                    Console.WriteLine("{0} {1} {2} Employee cannot retire due to lack of experience.", id, WAge, WExp);
                else if (WAge < 55 && WExp >= 20)
                    Console.WriteLine("{0} {1} {2} Employee cannot retire due to being underage.", id, WAge, WExp);
                else if (WAge < 55 && WExp < 20)
                    Console.WriteLine("{0} {1} {2} Employee cannot retire due to being underage and lack of experience.", id, WAge, WExp);
                break;
        }
    }
}
}

这是fileIn:

1235 W 45 20
2536 W 55 21
5894 W 60 30 
4597 W 75 35
2597 S 35 10
5689 S 40 20
5489 W 55 39
5872 M 60 40
5569 M 55 25
5566 W 80 20
8865 M 59 35
5598 S 65 35

以下是错误:

Severity    Code    Description Project File    Line    Suppression State
Error   CS7036  There is no argument given that corresponds to the required    formal parameter 'WAge1' of 'Program.UpdateTotals(int, double, double, double, double, double, double, double, double, double, double, double, double, double, double, double, double, double, double, double, double, double, double, double)'  Program9.1  C:\Users\Programing 1\Soluation 1\Program 9\Program9.1\Program9.1\Program.cs    37  Active
Severity    Code    Description Project File    Line    Suppression State
Error   CS7036  There is no argument given that corresponds to the required formal parameter 'WAge1' of 'Program.CalcAvg(int, double, double, double, double, double, double, double, double, double, double, double)'  Program9.1  C:\Users\Programing 1\Soluation 1\Program 9\Program9.1\Program9.1\Program.cs    39  Active
Severity    Code    Description Project File    Line    Suppression State
Warning CS0649  Field 'Program.WElig' is never assigned to, and will always have its default value  Program9.1  C:\Users\Programing 1\Soluation 1\Program 9\Program9.1\Program9.1\Program.cs    20  Active

1 个答案:

答案 0 :(得分:2)

  1. 您在Main方法中有一个UpdateTotals();来电,但这个UpdateTotals不是无参数的(实际上远非它,它需要一堆必需的参数。)
  2. CalcAvg
  3. 也是如此
  4. 这只是一个警告,代码仍然可以编译而不修复此问题。
  5. 关于你的设计:我更好的选择是你的方法获取一个对象,并让这个对象定义你想要做的事情。在一种方法上拥有这么多参数只是在以后的问题中提出问题。如果您发现它们都是相同类型且参数的位置无关紧要,那么您应该传入该类型的数组。

    void UpdateTotals(int WAge1, double WAge2, double WAge3, double WAge4, double WAge5, double WAge6, double WAge7, double WAge8, double WAge9, double WAge10, double WAge11, double WAge12, double WExp1, double WExp2, double WExp3, double WExp4, double WExp5, double WExp6, double WExp7, double WExp8, double WExp9, double WExp10, double WExp11, double WExp12)
    

    会变成

    void UpdateTotals(int WAge1, double[] WAges);
    

    如果WAge1实际上也是双倍,那么将整个事情改为

    void UpdateTotals(double[] WAges);
    

    如果你有多个数组,那么定义一个新类型,将其作为属性并传递它。

    public class SomeContainer {
      public double[] WAges {get;set;}
      public double[] WExps {get;set;}
    }
    
    void UpdateTotals(SomeContainer wContainer);
    

    @ Abion47指出,也许WAge和WExp是相关的。在这种情况下,更新模型以反映该模型。

    public class WContainer {
      public double WAge {get;set;}
      public double WExp {get;set;}
    }
    
    void UpdateTotals(WContainer[] ws);