1

我是 c# 的新手,所以请原谅我的基本问题,但是您如何解决“方法 'SaveData' 没有重载需要 8 个参数”的问题?

这是我的代码:

using System;
using System.IO;
namespace Assignment2partA
{
    class Program
    {
        const int MAXCUSTOMERS = 1;
        const float PEAK_RATE = 0.50F;
        const float OFF_PEAK_RATE = 0.18F;
        const float STANDARD_RATE = 0.32F;
        const float GST_RATE = 0.10F;
        const string PEAK_SYM = "P";
        const string STANDARD_SYM = "S";

        static void Main()
        {



            string[] CustomerNumber = new string[MAXCUSTOMERS];
            string[] CustomerName = new string[MAXCUSTOMERS];
            string[] CustomerAddress = new string[MAXCUSTOMERS];
            string[] CustomerRateType = new string[MAXCUSTOMERS];

            float[] Peak = new float[MAXCUSTOMERS];
            float[] OffPeak = new float[MAXCUSTOMERS];
            float[] Standard = new float[MAXCUSTOMERS];

            int currentcustomer = 0;
            string menuSelection = "";


            while (menuSelection != "x")
            {
                menuSelection = Menu();
                switch (menuSelection)
                {
                    case "s":
                        SaveData(CustomerNumber, CustomerName, CustomerAddress, CustomerRateType, Peak, OffPeak, Standard, ref currentcustomer);
                        break;

                    case "a":
                        AddCustomer(CustomerNumber, CustomerName, CustomerAddress, CustomerRateType, Peak, OffPeak, Standard, MAXCUSTOMERS, ref currentcustomer);
                        break;

                    case "f":
                        FindCustomer(CustomerNumber, CustomerName, CustomerAddress, CustomerRateType, Peak, OffPeak, Standard, currentcustomer, CustomerName.ToString());
                        break;

                    case "u":
                        Update(CustomerNumber, CustomerName, CustomerAddress, CustomerRateType, Peak, OffPeak, Standard, currentcustomer, CustomerName.ToString());
                        break;

                    case "e":
                        Electricity(CustomerNumber, CustomerName, CustomerAddress, CustomerRateType, Peak, OffPeak, Standard, currentcustomer);
                        break;

                    case "b":
                        Bills(CustomerNumber, CustomerName, CustomerAddress, CustomerRateType, Peak, OffPeak, Standard, ref currentcustomer);
                        break;




                    case "c":
                        Clear(CustomerNumber, CustomerName, CustomerAddress, CustomerRateType, Peak, OffPeak, Standard, currentcustomer);
                        break;

                    case "x":
                        break;
                }
            }
        }

        static string Menu()
        {
            string menuSelection = "";
            Console.WriteLine("What would you like to do?");
            Console.WriteLine("Add a Customer? (a)");
            Console.WriteLine("Find a Customer? (f)");
            Console.WriteLine("Update a Customer Record? (u)");
            Console.WriteLine("Enter Electricity Consumption data? (e)");
            Console.WriteLine("Calculate Bills? (b)");
            Console.WriteLine("Save data to file? (s)");
            Console.WriteLine("Clear Consuption data? (c)");
            Console.WriteLine("Exit? (x)");
            Console.Write("Enter your response >");
            menuSelection = Console.ReadLine();

            return menuSelection;
        }
        static void AddCustomer(string[] CustomerNumber, string[] CustomerName, string[] CustomerAddress, string[] CustomerRateType, float[] Peak, float[] OffPeak, float[] Standard, int MaxCustomers, ref int CurrentCustomer)
        {
            if (CurrentCustomer < MaxCustomers)
            {


                Console.WriteLine("Enter Customer Number");
                Console.Write("Customer Number  > ");
                CustomerNumber[CurrentCustomer] = Console.ReadLine();
                if (CustomerNumber[CurrentCustomer] == "")
                {
                    Console.WriteLine("Error : Customer Number cannot be empty,");
                }
                // Get the Customer Name and exit is an error is detected
                Console.Write("Customer Name    > ");
                CustomerName[CurrentCustomer] = Console.ReadLine();
                if (CustomerName[CurrentCustomer] == "")
                {
                    Console.WriteLine("Error : Customer Name cannot be empty,");
                }
                // Get the Customer Address and exit is an error is detected
                Console.Write("Address          > ");
                CustomerAddress[CurrentCustomer] = Console.ReadLine();
                if (CustomerAddress[CurrentCustomer] == "")
                {
                    Console.WriteLine("Error : Address cannot be empty,");
                }
                // Get the Rate Type and exit is an error is detected
                while (CustomerRateType[CurrentCustomer] != PEAK_SYM && CustomerRateType[CurrentCustomer] != STANDARD_SYM)
                {
                    Console.Write("Standard Rate (S) or Peak/Off Peak (P) >");
                    CustomerRateType[CurrentCustomer] = Console.ReadLine().ToUpper();
                    if (CustomerRateType[CurrentCustomer] != PEAK_SYM && CustomerRateType[CurrentCustomer] != STANDARD_SYM)
                    {
                        Console.WriteLine("Error : The Rate Type must be either S or P.");
                    }
                }

            }
          } 
        static int Search(string[] CustomerName, int Count, string id)
        {
            int record;
            for (record = 0; record < Count; record++)
                if (CustomerName[record] == id) break;

            if (record != Count) return -1; // not found
            else return record;                  // return record number
        }
        static void FindCustomer(string[] CustomerNumber, string[] CustomerName, string[] CustomerAddress, string[] CustomerRateType, float[] Peak, float[] OffPeak, float[] Standard, int CurrentCustomer, string CustomerSearch)
        {
            Console.WriteLine("Enter Customer Name: ");

            CustomerSearch = Console.ReadLine();
            int record = Search(CustomerName, CurrentCustomer, CustomerSearch);

            if (record == -1)
            {
                Console.WriteLine("Error! There is no customer that contains that name. ");
            }
            else
            {
                Console.WriteLine(CustomerNumber[record]);
                Console.WriteLine(CustomerName[record]);
                Console.WriteLine(CustomerAddress[record]);
                Console.WriteLine(CustomerRateType[record]);

                if (CustomerRateType[record] == PEAK_SYM)
                {
                    Console.WriteLine(Peak[record]);
                    Console.WriteLine(OffPeak[record]);
                }
                else
                {
                    Console.WriteLine(Standard[record]);
                }

            }
        }

        static void Bills(string[] CustomerNumber, string[] CustomerName, string[] CustomerAddress, string[] CustomerRateType, float[] Peak, float[] OffPeak, float[] Standard, ref int CurrentCustomer)
        {
            float TotalBilled = 0.0F;
            float TotalUsage = 0.0F;
            float AverageBilled = 0.0F;
            float AverageUsage = 0.0F;

            int record;
            CurrentCustomer = CustomerName.Length;

            // Calculation variables
            float PeakCharge = 0.0F;
            float OffPeakCharge = 0.0F;
            float TotalCharge = 0.0F;
            float TotalPlusGST = 0.0F;

            for (record = 0; record < CurrentCustomer; record++)
            {
                if (CustomerRateType[record] == PEAK_SYM)
                {
                    PeakCharge = Peak[record] * PEAK_RATE;
                    OffPeakCharge = OffPeak[record] * OFF_PEAK_RATE;
                    TotalCharge = PeakCharge + OffPeakCharge;
                    // Keep track of the usage
                    TotalUsage = TotalUsage + (Peak[record] + OffPeak[record]);
                }
                else
                {
                    TotalCharge = Standard[record] * STANDARD_RATE;
                    // Keep track of the usage
                    TotalUsage = TotalUsage + Standard[record];
                }

                TotalPlusGST = TotalCharge + TotalCharge * GST_RATE;
                // Keep track of the total billed
                TotalBilled = TotalBilled + TotalPlusGST;

                Console.WriteLine("===================================");
                Console.WriteLine("Electricity Bill for Customer {0}", CustomerNumber[record]);
                Console.WriteLine("===================================");
                Console.WriteLine("Name          : {0}", CustomerName[record]);
                Console.WriteLine("Address       : {0}", CustomerAddress[record]);
                if (CustomerRateType[record] == PEAK_SYM)
                {
                    Console.WriteLine("Peak          : {0,7:C}", PeakCharge);
                    Console.WriteLine("Off Peak      : {0,7:C}", OffPeakCharge);
                }

                Console.WriteLine("Total         : {0,7:C}", TotalCharge);
                Console.WriteLine();
                Console.WriteLine("Including GST : {0,7:C}", TotalPlusGST);
                Console.WriteLine("===================================");
            }
            AverageBilled = TotalBilled / CurrentCustomer;
            AverageUsage = TotalUsage / CurrentCustomer;
            // Display the averages
            Console.WriteLine("Average amount billed is {0:C}.", AverageBilled);
            Console.WriteLine("Average consumption per customer is {0}.", AverageUsage);

        }
        static void Update(string[] CustomerNumber, string[] CustomerName, string[] CustomerAddress, string[] CustomerRateType, float[] Peak, float[] OffPeak, float[] Standard, int CurrentCustomer, string CustomerSearch)
        {
            string Update;
            Console.WriteLine("Enter Customer Name: ");

            CustomerSearch = Console.ReadLine();
            int record = Search(CustomerName, CurrentCustomer, CustomerSearch);

            if (record == -1)
            {
                Console.WriteLine("Error! There is no customer that contains that name. ");
            }
            else
            {
                Console.WriteLine(CustomerNumber[record]);
                Console.Write("Do you want to update this record? (Y/N): ");
                Update = Console.ReadLine();
                if (Update == "Y")
                {
                    Console.Write("Please enter new value: ");
                    CustomerNumber[record] = Console.ReadLine();
                }
                Console.WriteLine(CustomerName[record]);
                Console.Write("Do you want to update this record? (Y/N): ");
                Update = Console.ReadLine();
                if (Update == "Y")
                {
                    Console.Write("Please enter new value: ");
                    CustomerName[record] = Console.ReadLine();
                }
                Console.WriteLine(CustomerAddress[record]);
                Console.Write("Do you want to update this record? (Y/N): ");
                Update = Console.ReadLine();
                if (Update == "Y")
                {
                    Console.Write("Please enter new value: ");
                    CustomerAddress[record] = Console.ReadLine();
                }
                Console.WriteLine(CustomerRateType[record]);

                if (CustomerRateType[record] == PEAK_SYM)
                {
                    Console.WriteLine(Peak[record]);
                    Console.WriteLine(OffPeak[record]);
                }
                else
                {
                    Console.WriteLine(Standard[record]);

                }
            }
        }
        static void Electricity(string[] CustomerNumber, string[] CustomerName, string[] CustomerAddress, string[] CustomerRateType, float[] Peak, float[] OffPeak, float[] Standard, int CurrentCustomer)
        {
            CurrentCustomer = CustomerName.Length;
            int record;
            for (record = 0; record < CurrentCustomer; record++)
            {
                Console.WriteLine(CustomerName[record]);

                if (CustomerRateType[record] == PEAK_SYM)
                {
                    Console.WriteLine("Enter Peak Usage: ");
                    Peak[record] = float.Parse(Console.ReadLine());
                    Console.WriteLine("Enter Off Peak Usage: ");
                    OffPeak[record] = float.Parse(Console.ReadLine());
                }
                else
                {
                    Console.WriteLine("Enter Standard Usage: ");
                    Standard[record] = float.Parse(Console.ReadLine());
                }



            }
        }




        static void Clear(string[] CustomerNumber, string[] CustomerName, string[] CustomerAddress, string[] CustomerRateType, float[] Peak, float[] OffPeak, float[] Standard, int CurrentCustomer)
        {
            CurrentCustomer = CustomerName.Length;
            int record;
            for (record = 0; record < CurrentCustomer; record++)
            {
                Peak[record] = 0.0f;
                OffPeak[record] = 0.0f;
                Standard[record] = 0.0f;

            }




        }
        static void SaveData(string CustomerNumber, string CustomerName, string CustomerAddress, string CustomerRateType, float Peak, float OffPeak, float Standard)
        {
            FileStream fin;
            string str;
            string[] data = null;
            // Open a file for reading
            try
            {
                fin = new FileStream("billingdata.txt", FileMode.Open);
            }
            // Report an error if file cannot be opened
            catch (IOException exc)
            {
                Console.WriteLine(exc.Message);
                return;
            }

            StreamReader fstr_in = new StreamReader(fin);
            string str1 = "";
            StreamWriter fstr_out;
            try
            {
                fstr_out = new StreamWriter("billingdata.txt");
            }
            catch (IOException exc)
            {
                Console.WriteLine(exc.Message);
                return;
            }
            try
            {
                fstr_out.WriteLine(CustomerNumber, CustomerName, CustomerAddress, CustomerRateType);
            }
            catch (IOException exc)
            {
                Console.WriteLine(exc.Message);
                return;
            }
            fstr_out.Close();
        }

    }
}
4

4 回答 4

3

您正在尝试将八个参数传递给SaveData只能接受七个参数的方法,而您必须remove从调用中传递一个不需要的参数,或者在方法定义中再添加一个参数。

方法调用,试图传递八个参数,

SaveData(CustomerNumber, CustomerName, CustomerAddress,
          CustomerRateType, Peak, OffPeak, 
          Standard, ref currentcustomer);

具有七个参数的方法定义。

static void SaveData(string CustomerNumber, string CustomerName, string CustomerAddress,     
                     string CustomerRateType, float Peak, float OffPeak, 
                     float Standard)
于 2013-10-12T13:54:07.593 回答
3

这是您的方法声明:

static void SaveData(string CustomerNumber, string CustomerName, 
                     string CustomerAddress, string CustomerRateType,
                     float Peak, float OffPeak, float Standard)

这是您的调用:

SaveData(CustomerNumber, CustomerName, CustomerAddress, 
         CustomerRateType, Peak, OffPeak, Standard, ref currentcustomer);

编译器告诉您它们不匹配 - 基本上,它看起来像是最后一个参数,这是问题所在。

所以你有三个选择:

  • ref currentcustomer在调用结束时删除参数
  • 更改SaveData以添加适当的参数
  • 更改SaveData返回客户更改呼叫:

    currentcustomer = SaveData(CustomerNumber, CustomerName, 
         CustomerAddress, CustomerRateType, Peak, OffPeak, Standard);
    

我还建议更改参数和局部变量名称以遵循 .NET 命名约定。

于 2013-10-12T13:57:54.677 回答
2

SaveData需要 7 个参数,你传递了 1 个额外的参数。

您的代码还有另一个问题,在发送 4 个数组和 3 个数组时SaveData需要 4 个string值和 3 个值:floatstring[]float[]

    string[] CustomerNumber = new string[MAXCUSTOMERS];
    string[] CustomerName = new string[MAXCUSTOMERS];
    string[] CustomerAddress = new string[MAXCUSTOMERS];
    string[] CustomerRateType = new string[MAXCUSTOMERS];

    float[] Peak = new float[MAXCUSTOMERS];
    float[] OffPeak = new float[MAXCUSTOMERS];
    float[] Standard = new float[MAXCUSTOMERS];

    ...

    SaveData(CustomerNumber, CustomerName, CustomerAddress, CustomerRateType, Peak, OffPeak, Standard, ref currentcustomer);

您需要从每个数组中传递 1 个元素,例如CustomerNumber[i] where i = some array index

于 2013-10-12T13:54:26.380 回答
0

你的方法SAVEDATA,它只需要 7 个参数,你传递 8 个。

错误消息是不言自明的。

传递这么多参数不是一个好习惯,创建一个具有所有参数类型的 poco 类并传递类类型的对象

于 2013-10-12T13:55:02.770 回答