Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is it possible to iterate list with for loop only?

Here is the class in C# in console program

    public class Person
    {
        public string Name;
        public int BirthYear;
        public int Age(int birthYear)
        {
            DateTime presents = DateTime.Now;
            int presentAge = presents.Year - birthYear;
            return presentAge;
        }

    }

and also the main program

static void Main(string[] args)
        {
            Console.WriteLine("Input peoples: ");
            int people = Convert.ToInt32(Console.ReadLine());
            Person a = new Person();
            for(int i = 0; i < people; i++)
            {
                Console.WriteLine("Person {0}", i + 1);
                Console.Write("Enter the name: ");
                a.Name = Console.ReadLine();
                Console.Write("Enter the birth year: ");
                a.BirthYear = Convert.ToInt32(Console.ReadLine());
                int present = a.Age(a.BirthYear);
                Console.WriteLine("Hello {0}, your age is {1} years old", a.Name, present);
            }
        }

I inputted 2 people and the results are like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Person 1
Enter the name: Lu Bu
Enter the birth year: 1998
Hello Lu Bu, your age is 23 years old
Person 2
Enter the name: Diao Chan
Enter the birth year: 2000
Hello Diao Chan, your age is 21 years old

I wanna achieve the result like this:

Person 1
Enter the name: Lu Bu
Enter the birth year: 1998
Person 2
Enter the name: Diao Chan
Enter the birth year: 2000
Hello Lu Bu, your age is 23 years old
Hello Diao Chan, your age is 21 years old

Is it possible to achieve with for loop only or is it must with List<>?

PS: The list in the question i mean isn’t List<> though

>Solution :

You can store the information provided (hellos) and print it in the end. You don’t have to use List<string>, it can be any collection (say, Queue<string>) or even a StringBuilder:

        StringBuilder hellos = new StringBuilder();

        for(int i = 0; i < people; i++)
        {
            Console.WriteLine("Person {0}", i + 1);
            Console.Write("Enter the name: ");
            a.Name = Console.ReadLine();
            Console.Write("Enter the birth year: ");
            a.BirthYear = Convert.ToInt32(Console.ReadLine());
            int present = a.Age(a.BirthYear);

            // Instead of printing, we collect the data...
            if (hellos.Length > 0)
              hellos.AppendLine();

            hellos.Append($"Hello {a.Name}, your age is {present} years old");
        }

        // ...and after the loop we print out all the data colelcted 
        Console.WriteLine(hellos);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading