简体   繁体   English

C# 循环内的输入

[英]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.所以我现在有一个学校项目,我正在尝试创建一段代码,用户输入学生姓名等,然后列出用户输入的所有学生 output。到目前为止,我有基本的部分代码有效但似乎无法弄清楚如何一一命名它们。 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.如果您还没有了解 collections(数组或列表),那么从技术上讲,您可以使用 ONE String 变量来保存所有值。

Just use a second, temp variable to hold the current one, and use String Concatenation to accumulate all of them together:只需使用第二个临时变量来保存当前变量,然后使用String Concatenation将它们全部累加在一起:

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 .但是,如果您希望将来能够轻松访问这些个人名称,那么最好使用ArrayList

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>

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.不知道您是否受限于某些标准,例如列表或 Arrays。但是上面提到的一张海报使用串联,我还会考虑将转义字符添加到字符串变量的串联中。 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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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