[英]C# how to average a array input
I Have the first part of my code pretty complete and I got the gist of the rest, I'm just not sure how to put it all together. 我的代码的第一部分非常完整,我得到了其余部分的要点,我只是不确定如何将它们放在一起。 Here is the first part
这是第一部分
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);
}
}
}
}
The second part is to ask the user what they want to do with the arrays 第二部分是询问用户他们想要对阵列做什么
enter an a or A to search account numbers enter ab or B to average the accounts enter an x or X to exit program 输入a或A来搜索帐号输入ab或B来平均帐户输入x或X以退出程序
the search part I can use something like this: 搜索部分我可以使用这样的东西:
public void search Accounts()
{
for(int x = 0; x < validValues.Lenth; ++x)
{
if(acctsearched == validValues[x])
{
isValidItem = true;
acctNumber = Number[x];
}
}
And I can use a while loop with a bool true/false to close out. 我可以使用带有bool true / false的while循环来关闭。
I'm not sure how to get the average balance. 我不确定如何获得平均余额。 I keep getting errors like "can't implicitly change int[] to int"
我一直得到像“不能隐式地将int []更改为int”之类的错误
Any help with this would very much appreciated. 对此有任何帮助非常感谢。
Sounds like you have an List of AccountInfo
. 听起来你有一个
AccountInfo
列表。 Are you able to use LINQ? 你能使用LINQ吗?
To get the average, you can do this with LINQ: 要获得平均值,可以使用LINQ执行此操作:
double avg = accounts.Select(x=>x.Balance).Average();
To search for a given acct, you can do something like this: 要搜索给定的acct,您可以执行以下操作:
var foundAcct = accounts.SingleOrDefault(x=>x.Number==someSearchNum);
For this to work(and create methods for these 2 actions), you'd need to move the List<AccountInfo> accounts
out of the Main
and be declared in the class. 要使其工作(并为这两个操作创建方法),您需要将
List<AccountInfo> accounts
移出Main
并在类中声明。
With LINQ, you'll required .NET 3.5 or greater. 使用LINQ,您将需要.NET 3.5或更高版本。
using System; 使用系统; namespace AssignmentWeek3 { class Accounts { // private class members, creating new arrays private int [] AcctNumber = new int[5];
namespace AssignmentWeek3 {class Accounts {//私有类成员,创建新数组private int [] AcctNumber = new int [5]; private double [] AcctBalance = new double[5];
private double [] AcctBalance = new double [5]; private string [] LastName = new string[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
class week3_assignment //Main class { static void Main() { char entry = '0'; class week3_assignment //主类{static void Main(){char entry ='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;
}
}
}
}
} } }}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.