【问题标题】:c# search array - No overload for methodc#搜索数组 - 方法没有重载
【发布时间】:2011-07-28 21:00:44
【问题描述】:

我在尝试编译我的程序时不断收到错误消息,我在代码的搜索部分遇到问题。不知道我做错了什么。

这是我在编译时遇到的错误:

week3.cs(97,17):错误 CS1501:否 方法“searchAccounts”的重载 需要 1 个参数 week3.cs(27,21): (相关符号的位置 以前的错误)

这是我的全部代码:

using System;

class Accounts
    {
        // private class members
        const int arrayLength = 5;
        private int [] Account = new int[arrayLength]; //create arrays
        private double [] AcctBalance = new double[arrayLength];
        private string [] LastName = new string[arrayLength];

        // fill all three parallel arrays with input
        public void fillAccounts(int[] Account, double[] AcctBalance, string[] accountNameArray)
        {
            int arrayLength = 0;
            for (int i = 0; i < Account.Length; i++)
            {
                Console.Write("Enter integer account number ");
                Account[arrayLength] = Convert.ToInt32(Console.ReadLine());
                Console.Write("Enter account balance ");
                AcctBalance[arrayLength] = Convert.ToDouble(Console.ReadLine());
                Console.Write("Enter account holder last name ");

            } //end for

        } //end fillAccounts method

        public void searchAccounts(int[] Account, double[] AccountBalance, string[] LastName)
        {
            int accountNum = 0;
            bool isValid = false;
            int x = 0;

            Console.Write("Please enter the account number you wish to search for ");
            accountNum = Convert.ToInt32(Console.ReadLine());

            while (x < Account.Length && accountNum != Account[x])
                ++x;
            if(x != Account.Length)
            {
                isValid = true;
            }
            if(isValid)
                Console.WriteLine("Account number " + Account[x] + " has a balance of " + AcctBalance[x]
                    + " and the account holder is " + LastName[x]);
            else
                Console.WriteLine("No such account as {0}", accountNum);


        }

        public void averageAccounts(int[] Account, double[] AcctBalance)
        {
            // compute and display average of all 5 bal as currency use length.
            int balanceCount = 0;
            double balance = AcctBalance[balanceCount];
            double balanceSum = 0;
            double averageBalance = 0;

            for (int i = 0; i < Account.Length; i++)
            {
                balanceSum = balanceSum + balanceCount;
            }
            averageBalance = (balanceSum/balanceCount);
        } //end averageAccounts method
    } //end public class accounts


    class account_assignment  //wrapper class for main
    {
        static void Main()
        {
        int[] Account; 
        double[] AcctBalance; 
        string[] LastName;
            char entry;
            char a, A;
            char b, B;
            char x, X;

            //instantiate one new Accounts object
            Accounts accounts = new Accounts();

            //call class methods to fill accounts Array
            accounts.fillAccounts(Account, AcctBalance, LastName);
            //menu detailing entnries to select search (a or A) average (b or B) exit (x or X)


            Console.WriteLine("*****************************************");
            Console.WriteLine("enter an a or A to search account numbers");
            Console.WriteLine("enter a b or B to average the accounts");
            Console.WriteLine("enter an x or X to exit program");
            Console.WriteLine("*****************************************");

            entry = Convert.ToChar(Console.ReadLine());

            if (entry == 'a' || entry == 'A')
                accounts.searchAccounts(Account);
            if (entry == 'b' || entry == 'B')
                accounts.averageAccounts(Account, AcctBalance);
            if (entry == 'x' || entry == 'X')
                Console.WriteLine("The program will now close");

            //internal documentation
        } //end main
    }

任何帮助将不胜感激。

【问题讨论】:

  • 有点离题,但我强烈建议您创建一个Account 类并使用List&lt;Account&gt;Dictionary&lt;int,Account&gt; 类型的列表。这不仅是更好的 OO 实践,而且您还可以轻松掌握 Linq 的所有功能,因此您不必从头开始编写方法为 averageAccounts

标签: c# arrays search methods


【解决方案1】:

行:

accounts.searchAccounts(Account);

正在使用一个参数调用 searchAccounts。

而 searchAccounts 有这个要求:

public void searchAccounts(int[] Account, double[] AccountBalance, string[] LastName)

【讨论】:

    【解决方案2】:

    替换

    accounts.searchAccounts(Account);
    

    accounts.searchAccounts(Account, AcctBalance, LastName);
    

    正如现在声明的searchAccounts 需要三个参数,而不仅仅是您要传递的那个——另外两个不是可选的(尽管您似乎根本没有使用searchAccounts() 中的帐户余额)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-24
      • 2014-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多