2011-04-03 42 views
0

我有我的代碼的第一部分相當完整,我得到了其餘的主旨,我只是不知道如何把它放在一起。這裏是第一部分C#如何平均數組輸入

using System; 
namespace ConsoleApplication1 
{ 
    class Program 
    { 
     public class AccountInfo 
     { 
      public int Number { get; set; } 
      public double Balance { get; set; } 
      public string LastName { get; set; } 
     } 

     static void Main(string[] args) 
     { 
      List<AccountInfo> accounts = new List<AccountInfo>(); 

      for (int index = 1; index < 6; index++) 
      { 
       AccountInfo acc = new AccountInfo(); 

       Console.Write("Enter account number: " + index.ToString() + ": "); 
       acc.Number = int.Parse(Console.ReadLine()); 
       Console.Write("Enter the account balance: "); 
       acc.Balance = double.Parse(Console.ReadLine()); 
       Console.Write("Enter the account holder last name: "); 
       acc.LastName = Console.ReadLine(); 

       accounts.Add(acc); 
      } 

     } 
    } 
} 

第二部分是問用戶他們想要與陣列


進入一個或一個搜索賬號 輸入AB或B做什麼平均 輸入x或X退出程序賬目


我可以使用像這樣的搜索部分:

public void search Accounts() 
{ 
    for(int x = 0; x < validValues.Lenth; ++x) 
    { 
     if(acctsearched == validValues[x]) 
     { 
      isValidItem = true; 
      acctNumber = Number[x]; 
     } 
     } 

而且我可以用一個帶有bool true/false的while循環來關閉。

我不知道如何獲得平均餘額。我不斷地收到錯誤,如「不能隱式地改變int [] int」

任何幫助,這將非常讚賞。

回答

0

using System; 命名空間AssignmentWeek3 { 類帳戶 { //私有類成員,創造新的陣列 私人INT [] ACCTNUMBER =新INT [5]; private double [] AcctBalance = new double [5]; private string [] LastName = new string [5];

public void fillAccounts()//fill arrays with user input 
    { 
     int x = 0; 
     for (int i = 0; i < AcctNumber.Length; i++) 
     { 
      Console.Write("Enter account number: ");    
      AcctNumber[x] = Convert.ToInt32(Console.ReadLine()); 
      Console.Write("Enter account balance: "); 
      AcctBalance[x] = Convert.ToDouble(Console.ReadLine()); 
      Console.Write("Enter account holder last name: "); 
      LastName[x] = Convert.ToString(Console.ReadLine()); 
      x++; 
     } 

    } 

    public void searchAccounts() //search account method to be called later in main() 
    { 
     int accountNum = 0; 
     bool isValid = false; 
     int x = 0; 

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


     while (x < AcctNumber.Length && accountNum != AcctNumber[x]) 
      ++x; 
     if(x != AcctNumber.Length) 
     { 
     isValid = true; 
     } 
     if(isValid){ 
     Console.WriteLine("AcctNumber: {0}  Balance: {1:c} Acct Holder: {2}", AcctNumber[x], AcctBalance[x], LastName[x]); 
     } 
     else{ 
      Console.WriteLine("You entered an invalid account number"); 
     }      
    } 

    public void averageAccounts()//averageAccounts method to be store for later use 
    { 
     // compute and display average of all 5 bal as currency use length. 
     int x = 0; 
     double balanceSum = 0; 
     double Avg = 0; 

     for (x = 0; x < 5; x++) 
     { 
      balanceSum = balanceSum + AcctBalance[x]; 
     } 
     Avg = (balanceSum/x); 
     Console.WriteLine("The average balance for all accounts is {0:c}", Avg); 
    } 

} //end public class Accounts 

類week3_assignment //主類 { 靜態無效的主要() { 炭條目= '0';

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

     //call class methods to fill Accounts 
     accounts.fillAccounts(); 

     //menu with input options 
     bool Exit = false; 
     while (!Exit) 
     { 
     while (entry != 'x' && entry != '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()); 

       switch (entry) //set switch 
       { 
        case 'a': 
        case 'A': 
         accounts.searchAccounts();//calls search accounts method 
         break; 
        case 'b': 
        case 'B': 
         accounts.averageAccounts();//calls average accounts method 
         break; 
        case 'x': 
        case 'X': 
         (Exit) = true; 
         break; 
        default: 
         Console.WriteLine("That is not a valid input"); 
         break; 
       } 
      } 
     }  
    } 

}}

2

聽起來像你有一個列表AccountInfo。你能夠使用LINQ嗎?

爲了得到平均,你可以用LINQ做到這一點:

double avg = accounts.Select(x=>x.Balance).Average(); 

要搜索一個給定的ACCT,你可以做這樣的事情:

var foundAcct = accounts.SingleOrDefault(x=>x.Number==someSearchNum); 

對於這個工作(和爲這兩個動作創建方法),您需要將List<AccountInfo> accounts移出Main並在課程中聲明。

使用LINQ,您將需要.NET 3.5或更高版本。

+4

http://www.albahari.com/nutshell/linqbridge.aspx – MusiGenesis 2011-04-03 01:54:19

+0

@Musi:太好了!沒有意識到他們已經達到了這個程度。阿爾巴哈里人很棒。 – 2011-04-03 01:57:02

+0

@MusiGenesis - OMG有史以來最好的鏈接 – Pandincus 2011-04-03 01:58:56