将类的对象分配给数组元素,将for循环分配给数组的元素 [英] Assign objects of class to elements of array and a for loop to call elements of array

查看:120
本文介绍了将类的对象分配给数组元素,将for循环分配给数组的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我在这里很新。这是我的第二篇文章。我正在学习如何使用c#,我想知道这里是否有人可以帮助我使用数组。



- 首先,我想声明并创建一个包含5个元素的数组。我创建了另一个名为Account的类,我希望该数组的类型为Account。



- 第二,我想分配现有的我创建的类的对象(Account)到数组的元素



- 最后,我想知道如何使用for循环从数组的每个元素调用方法显示。



我确实尝试了一切,我似乎无法得到这个。 我希望我的新代码能够获得与旧代码相同的结果,但是使用数组(将现有对象分配给数组元素)和for循环来调用每个数组元素的显示方法。 />


我首先要知道我需要为最大尺寸声明一个常数,所以我开始写这个



public const int MAX_SIZE = 5;



然后,为了使用Account类的元素声明一个数组,我在Main下写了这个$



int [] AccountArray =新帐户[5];





此时我我输了。我尝试在网上和我的教科书中查找,但我找不到任何特定于我想要完成的例子。这是我要修改的原始代码





Hi everyone,

I''m fairly new here. This is my second post. I''m just learning how to use c#, and I was wondering if someone here can help me with arrays.

- First, I want to declare and create an array of 5 elements. I created another class called Account, and I want the array to be of type Account.

- Second, I want to assign the existing objects of the class I made (Account) to the elements of the array

- Lastly, I''m trying to figure out how to use a for loop to call the method display from each element of the array.

I''ve literally tried everything, and I just cant seem to get this. I want my new code to get the same results as my old code, but using an array (assigning the existing objects to the arrays elements) and a for loop to call the display method from each of the arrays elements.

I know first I need to declare a constant for max size, so I started off writing this

public const int MAX_SIZE = 5;

Then, to have an array declared with elements of the Account class, I wrote this under Main

int[] AccountArray = new Account[5];


At this point I''m lost. I tried looking online and in my textbook, but I can''t find any examples specific to what I''d like to accomplish. Here is my original code that I want to modify


static void Main(string[] args)

     {


        //int acctNo;

        Account acct = new Account(100, 2.5); // account #1 with $100 and 2.5% annual rate


        int choice;
      // int accountNo;
       double bal, r, amt; // balance, annual rate, amount
       int y; // number of years
       double fut; // future value


       Console.WriteLine("\nPlease make your selection:");
       Console.WriteLine("   0 - create an account");
       Console.WriteLine("   1 - deposit money");
       Console.WriteLine("   2 - withdraw money");
       Console.WriteLine("   3 - add monthly interest");
       Console.WriteLine("   4 - calculate future value");
       Console.WriteLine(" or -1 to finish");

       choice = Convert.ToInt32(Console.ReadLine());


        while (choice != -1)
       {
           switch (choice)
           {
               case 0: // create account

                  // acct.AccountDisplay();

                   Console.WriteLine("Enter balance:");
                   bal = Convert.ToDouble(Console.ReadLine());
                   Console.WriteLine("Enter percent of annual rate:");
                   r = Convert.ToDouble(Console.ReadLine());
                   acct = new Account(bal, r);
                   acct.display();

                   break;
               case 1: // deposit
                   Console.WriteLine("Enter deposit amount:");
                   amt = Convert.ToDouble(Console.ReadLine());
                   acct.deposit(amt);
                   acct.display();
                   break;

               case 2: // withdraw
                   Console.WriteLine("Enter withdrawal amount:");
                   amt = Convert.ToDouble(Console.ReadLine());
                   acct.withdraw(amt);
                   acct.display();
                   break;

               case 3: // add monthly interest
                   acct.applyMonthlyInterest();
                   acct.display();
                   break;

               case 4: // calculate future value
                   Console.WriteLine("Enter number of years for future value calculation:");
                   y = Convert.ToInt32(Console.ReadLine());
                   fut = acct.futureBalance(y);
                   Console.WriteLine("In {0} years you will have {1:c}." , y, fut);

                   break;

           } // end switch

           // next choice

           Console.WriteLine("\nPlease make your selection:");
           Console.WriteLine("   0 - create an account");
           Console.WriteLine("   1 - deposit money");
           Console.WriteLine("   2 - withdraw money");
           Console.WriteLine("   3 - add monthly interest");
           Console.WriteLine("   4 - calculate future value");
           Console.WriteLine(" or -1 to finish");

           choice = Convert.ToInt32(Console.ReadLine());
       }
       Console.WriteLine("Press any key, and good bye...");
       Console.ReadKey();

推荐答案

100和2.5%年利率


int choice;
// int accountNo;
bal,r,amt; // 余额,年费率,金额
int y; // 年数
double fut; // 未来值


Console.WriteLine( \ n请选择:);
Console.WriteLine( 0 - 创建帐户);
Console.WriteLine( 1 - 存钱);
Console.WriteLine( 2 - 取钱);
Console.WriteLine( 3 - 添加每月兴趣);
Console.WriteLine( 4 - 计算未来值);
Console.WriteLine( 或-1表示完成);

choice = Convert.ToInt32(Console.ReadLine());


while (choice!= -1)
{
开关(选择)
{
案例 0 // 创建帐户

// acct.AccountDisplay();

Console.WriteLine( 输入余额:);
bal = Convert.ToDouble(Console.ReadLine());
Console.WriteLine( 输入年利率百分比:);
r = Convert.ToDouble(Console.ReadLine());
acct = new 帐户(bal,r);
acct.display();

break ;
case 1 // 存款
Console.WriteLine( 输入存款金额:);
amt = Convert.ToDouble(Console.ReadLine());
acct.deposit(amt);
acct.display();
break ;

case 2 / / 撤销
Console.WriteLine( 输入提款金额:);
amt = Convert.ToDouble(Console.ReadLine());
acct.withdraw(amt);
acct.display();
break ;

case 3 / / 添加每月利息
acct.applyMonthlyInterest();
acct.display();
break ;

case 4 / / 计算未来值
Console.WriteLine( 输入未来值计算的年数:);
y = Convert.ToInt32(Console.ReadLine());
fut = acct.futureBalance(y);
Console.WriteLine( 在{0}年,您将拥有{1:c}。,y,fut);

break ;

} // 结束开关

< span class =code-comment> // 下一个选择

Console.WriteLine( \ n请选择:);
Console.WriteLine( 0 - 创建帐户);
Console.WriteLine( 1 - 存钱);
Console.WriteLine( 2 - 取钱);
Console.WriteLine( 3 - 添加每月兴趣);
Console.WriteLine( 4 - 计算未来值);
Console.WriteLine( 或-1表示完成);

choice = Convert.ToInt32(Console.ReadLine());
}
Console.WriteLine( 按任意键,再见......< /跨度>);
Console.ReadKey();
100 and 2.5% annual rate int choice; // int accountNo; double bal, r, amt; // balance, annual rate, amount int y; // number of years double fut; // future value Console.WriteLine("\nPlease make your selection:"); Console.WriteLine(" 0 - create an account"); Console.WriteLine(" 1 - deposit money"); Console.WriteLine(" 2 - withdraw money"); Console.WriteLine(" 3 - add monthly interest"); Console.WriteLine(" 4 - calculate future value"); Console.WriteLine(" or -1 to finish"); choice = Convert.ToInt32(Console.ReadLine()); while (choice != -1) { switch (choice) { case 0: // create account // acct.AccountDisplay(); Console.WriteLine("Enter balance:"); bal = Convert.ToDouble(Console.ReadLine()); Console.WriteLine("Enter percent of annual rate:"); r = Convert.ToDouble(Console.ReadLine()); acct = new Account(bal, r); acct.display(); break; case 1: // deposit Console.WriteLine("Enter deposit amount:"); amt = Convert.ToDouble(Console.ReadLine()); acct.deposit(amt); acct.display(); break; case 2: // withdraw Console.WriteLine("Enter withdrawal amount:"); amt = Convert.ToDouble(Console.ReadLine()); acct.withdraw(amt); acct.display(); break; case 3: // add monthly interest acct.applyMonthlyInterest(); acct.display(); break; case 4: // calculate future value Console.WriteLine("Enter number of years for future value calculation:"); y = Convert.ToInt32(Console.ReadLine()); fut = acct.futureBalance(y); Console.WriteLine("In {0} years you will have {1:c}." , y, fut); break; } // end switch // next choice Console.WriteLine("\nPlease make your selection:"); Console.WriteLine(" 0 - create an account"); Console.WriteLine(" 1 - deposit money"); Console.WriteLine(" 2 - withdraw money"); Console.WriteLine(" 3 - add monthly interest"); Console.WriteLine(" 4 - calculate future value"); Console.WriteLine(" or -1 to finish"); choice = Convert.ToInt32(Console.ReadLine()); } Console.WriteLine("Press any key, and good bye..."); Console.ReadKey();


首先,如果要创建一个数组来存储Account对象,它必须是 Account 类型,而不是 int (实际上它也可以是例如System.Object类型,并且与类继承有关)。



要将对象分配给先前创建的数组的任何元素,您需要指定数组的索引,应该存储它的位置,如下所示:

Firstly, if you want to create an array to store Account objects in it, it has to be the Account type, not int (actually it could be also for example System.Object type and that''s related to class inheritance).

To assign an object to any element of the previously created array, you have you specify the index of the array, where it should be stored, like this:
Account[] accounts = new Account[MAX_SIZE];
Account tempAcc = new Account(); //just to represent any Account object you've created
accounts[3] = tempAcc;





要调用某种方法,你必须循环通过元素在数组中,您可以通过两种方式完成:



To call a certain method you have to loop thru the elements in the array, you can do it in 2 ways:

for(int i = 0; i < tempAcc.Length; i++)
{
accounts[i].YourDesiredMethod();
}











or

foreach(Account acc in accounts)
{
acc.YourDesiredMethod();
}


我不打算编写代码,但会尝试回答如何操作。



I am not going to write the code but will try to answer how to do it.

Quote:

- 首先,我要声明并创建一个包含5个元素的数组。我创建了另一个名为Account的类,我希望该数组的类型为Account。

- First, I want to declare and create an array of 5 elements. I created another class called Account, and I want the array to be of type Account.

Account[] accountArray  = new Account[MAX_SIZE]; // this is how you define an array in C#







Quote:



- 其次,我想分配我创建的类的现有对象(Account)到数组的元素


- Second, I want to assign the existing objects of the class I made (Account) to the elements of the array

来访问数组你可以使用数组的索引运算符,如下所示

to access the array you can use the index operator of the array like below

Account account = accountArray[0];

这将从第0个索引获取帐户对象。请记住,数组索引从零开始而不是一个。



this will get the account object from the 0th index. keep in mind that array index starts from zero not one.

引用:



- 最后,我正在试图弄清楚如何使用for循环从数组的每个元素调用方法显示。


- Lastly, I''m trying to figure out how to use a for loop to call the method display from each element of the array.



To迭代数组要么可以使用foreach方法,要么用于循环。


To Iterate the array either you can use foreach method or for loop.

foreach(Account account in accountArray)
{
 //use the account object to execute the member function eg
 account.deposit(amount);
}
or
for(int i=0;i<max_size;i++)>
{
 Account account = accountArray[i];
}


这篇关于将类的对象分配给数组元素,将for循环分配给数组的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆