简体   繁体   中英

how do I go about this project

Hey guys I need your help.So I was making a register, everything started of nice but I kind of ran into some trouble. I'm thinking it has something to do with the For Loop in my code but yet again who am I to determine I'm only a beginner but anyway, I don't know how else to address the problem. Here is my code

using System;
using System.Collections.Generic;

namespace MyPrograms
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("What is the number of children present: ");
            int presentStudents = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("---------------------");

            Console.WriteLine("Please Write their names down: ");


            List <string> Students = new List<string>();

            Students.Add(Console.ReadLine());
            Students.Sort();

            for (var i = presentStudents; i < Students.Count; i++)
            {
                Console.WriteLine(i++);
            }

            Console.ReadKey();
            
        }

    } 
}

i think that this should solve your Problem. The while loop checks if you have enterd the same amount of names as the presentStudents int.

What would you like to achive with the for loop?

    static void Main(string[] args)
    {
        Console.WriteLine("What is the number of children present: ");
        int presentStudents = Convert.ToInt32(Console.ReadLine());

        Console.WriteLine("---------------------");

        Console.WriteLine("Please Write their names down: ");

        List<string> Students = new List<string>();
        while (Students.Count < presentStudents)
        {
            Students.Add(Console.ReadLine());
            Students.Sort();

        }
        if (Students.Count == presentStudents)
        {
            Console.WriteLine("Thank you for entering the Names. - " + presentStudents + " Students are present.");
        }
        Console.ReadKey();

    }

I don't quite understand the actual problem but this will light some bulbs.

Console.WriteLine("What is the number of children present: ");
int presentStudents = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("---------------------");

Console.WriteLine("Please Write their names down: ");
// Gathering Information
List<string> Students = new List<string>();
for (int i = 0; i < presentStudents; i++)
{
     Console.Write($"{i+1}. Student = ");
     Students.Add(Console.ReadLine());
}

// Show Information
for (int i = 0; i < presentStudents; i++)
{
    Console.WriteLine($"{i+1}. {Students[i]}");
}
Console.WriteLine("Press any key to exit...");
Console.Read();

Or can try like

class Program
    {
        static void Main(string[] args)
        {
    
            Console.WriteLine("What is the number of children present: ");
            int presentStudents = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("---------------------");          

            Console.WriteLine("Please Write their names down: ");

            var studentsList = Console.ReadLine();

            List<string> Students = new List<string>();

            Students.AddRange(studentsList.Split(','));

            Students.Sort();

           
            for (var i = 0 ; i < presentStudents; i++)
            {
                Console.WriteLine(Students[i]);
            }


            // Or Try
        //for (var i = 0; i < Students.Count ; i++)
        //{
        //    Console.WriteLine(Students[i]);
        //}


            Console.ReadKey();
        }

The try input like: 5 then student1,student2,student3,student4,student5

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM