简体   繁体   中英

C# Inputs within a loop

So I have a school project at the moment, and I'm trying to create a piece of code where the user inputs names of students etc and then lists all the students the user entered as an output. So far I have the base part of the code working but can't seem to figure out how to name them one by one. I'll display the code below to see if any of you know of a fix, thanks so much!

I've tried many different ways to fix this but don't know how, so below I just attached the piece of code I first coded. Any help is super appreciated!

string Name = "";

// so here is where i get the number of times to loop the input student name question

Console.WriteLine("How many students names would you like to input?");
int NumberOfStudents = Convert.ToInt32(Console.ReadLine());

// here im looping by how many students the user wants to input

for (int i = 0; i < NumberOfStudents; i++)
{
    Console.WriteLine("Enter name for student number: " + i);
    Name = Console.ReadLine();
}

// but then of course here, it only inputs one student name (the last student entered)

Console.WriteLine(Name);
Console.ReadLine();

If you haven't learned about collections (Arrays or a List), then you could technically use your ONE String variable to hold all the values.

Just use a second, temp variable to hold the current one, and use String Concatenation to accumulate all of them together:

String Name = "";

for (int i = 0; i < NumberOfStudents; i++)
{
    Console.WriteLine("Enter name for student number: " + i);
    String curName = Console.ReadLine();
    if (Name == "")
    {
        Name = curName;
    }
    else
    {
        Name = Name + ", " + curName;
    }
}

Console.WriteLine(Name);

If you want to be able to easily access those individual names in the future, though, then you'd be better off with an Array or a List .

You can use array of string like this

string[] Names;

// so here is where i get the number of times to loop the input student name question

Console.WriteLine("How many students names would you like to input?");
int NumberOfStudents = Convert.ToInt32(Console.ReadLine());

Names = new string[NumberOfStudents];

// here im looping by how many students the user wants to input

for (int i = 0; i < NumberOfStudents; i++)
{
    Console.WriteLine("Enter name for student number: " + i);
    Names[i] = Console.ReadLine();
}

foreach (string name in Names)
{
    Console.WriteLine(name);
}
Console.ReadLine();

Or a List<string>

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

// so here is where i get the number of times to loop the input student name question

Console.WriteLine("How many students names would you like to input?");
int NumberOfStudents = Convert.ToInt32(Console.ReadLine());

// here im looping by how many students the user wants to input

for (int i = 0; i < NumberOfStudents; i++)
{
    Console.WriteLine("Enter name for student number: " + i);
    Names.Add(Console.ReadLine() ?? "The user did not enter a name");
}

foreach (string name in Names)
{
    Console.WriteLine(name);
}
Console.ReadLine();

Not knowing if you're limited to certain criteria or not such as Lists or Arrays. But one of the posters above mentioned using concatenation, I would also look at adding escape characters to the concatenation of a string variable. This can give it the look and feel of a list but is still a single string variable.

string Name = "";
string ListOfNames =""; // Will contain all the names entered as a single string.
// so here is where i get the number of times to loop the input student name question

Console.WriteLine("How many students names would you like to input?");
int NumberOfStudents = Convert.ToInt32(Console.ReadLine());

// here im looping by how many students the user wants to input

for (int i = 0; i < NumberOfStudents; i++)
{
    Console.WriteLine("Enter name for student number: " + i);
    Name = Console.ReadLine();
    // Adds the name entered to this other string variable we created and uses the
    // the escape character for a new line "\n"
    ListofNames += Name + "\n"; 
}

// but then of course here, it only inputs one student name (the last student entered)

// Prints the other string variable that contains names entered during the for loop.
Console.WriteLine(ListOfNames);                               
Console.ReadLine();

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