Home > Back-end >  Is it possible to iterate list with for loop only?
Is it possible to iterate list with for loop only?

Time:11-24

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:

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

CodePudding user response:

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);

CodePudding user response:

You could try to create a simple string variable and add the description of the people in each iteration of your for loop

Then you'll have to display the variable in the console after the loop with one simple line of code

static void Main(string[] args)
{
    // Writing in Console
    Console.WriteLine("Input peoples: ");

    // Reading Console
    int people = Convert.ToInt32(Console.ReadLine());

    Person a = new Person();

    // Here instanciate the new variable
    string finalMessage = "";

    for(int i = 0; i < people; i  )
    {
        // Writing in Console
        Console.WriteLine("Person {0}", i   1);
        Console.Write("Enter the name: ");

        // Reading Console
        a.Name = Console.ReadLine();

        // Writing in Console
        Console.Write("Enter the birth year: ");

        // Reading Console
        a.BirthYear = Convert.ToInt32(Console.ReadLine());

        int present = a.Age(a.BirthYear);

        // Here add the value to the variable instead of displaying it
        finalMessage  = String.Format("Hello {0}, your age is {1} years old\n", a.Name, present);
    }

    // Writing in Console
    Console.WriteLine(finalMessage);
}

Thx to Crowcoder for notifying that I deleted "String.Format("Hello..."), I modified this answer and added it back

  • Related