C# 如何通过读取用户输入将变量添加到列表中?
我是C#的初学者,希望通过添加到我的列表中。读取用户的输入C# 如何通过读取用户输入将变量添加到列表中?,c#,list,console,C#,List,Console,我是C#的初学者,希望通过添加到我的列表中。读取用户的输入 class Program { static void Main(string[] args) { List<int> list = new List<int>(); Console.WriteLine("Asking a random Question here"); // lacking Knowledge here **
class Program
{
static void Main(string[] args)
{
List<int> list = new List<int>();
Console.WriteLine("Asking a random Question here");
// lacking Knowledge here **
** = Convert.ToInt16(Console.ReadLine());
Console.ReadKey();
}
类程序
{
静态void Main(字符串[]参数)
{
列表=新列表();
Console.WriteLine(“在这里随意提问”);
//缺乏知识**
**=Convert.ToInt16(Console.ReadLine());
Console.ReadKey();
}
我想你想要
list.Add(Convert.ToInt32(Console.ReadLine()));
int
是一个32位的数字,对应于系统。Int32
;我想您需要
list.Add(Convert.ToInt32(Console.ReadLine()));
class Program
{
static void Main(string[] args)
{
List<int> list = new List<int>();
Console.WriteLine("Asking a random Question here");
// lacking Knowledge here **
** = Convert.ToInt16(Console.ReadLine());
Console.ReadKey();
}
int
是一个32位数字,对应于系统。Int32
class Program
{
static void Main(string[] args)
{
List<int> list = new List<int>();
Console.WriteLine("Asking a random Question here");
// lacking Knowledge here **
** = Convert.ToInt16(Console.ReadLine());
Console.ReadKey();
}
然后将号码添加到您的列表中,如下所示:
list.Add(number);
然后将号码添加到您的列表中,如下所示:
list.Add(number);
为了给这个问题提供一个详细的例子,这里有一个程序,它将平均标准输入中列出的数字,每行一个。它还展示了如何将给定的数字添加到列表中
using System;
using System.Collections.Generic;
namespace AverageNumbers
{
class MainClass
{
public static void Main (string[] args)
{
// Here is the list of numbers
List<int> numbers = new List<int>();
// Here are two variables to keep track
// of the input number (n), and the sum total (sum)
int n, sum = 0;
// This while loop waits for user input and converts
// that input to an integer
while (int.TryParse(Console.ReadLine(), out n))
{
// Here we add the entered number to the sum
sum += n;
// And to the list to track how many we've added
numbers.Add(n);
}
// Finally we make sure that we have more than 0 numbers that we're summing and write out their average
Console.WriteLine("Average: " + (numbers.Count > 0? sum / numbers.Count : 0));
}
}
}
使用系统;
使用System.Collections.Generic;
命名空间平均数
{
类主类
{
公共静态void Main(字符串[]args)
{
//这是数字清单
列表编号=新列表();
//这里有两个要跟踪的变量
//输入数(n)和总和(sum)的
int n,和=0;
//此while循环等待用户输入并转换
//将该输入转换为整数
while(int.TryParse(Console.ReadLine(),out n))
{
//在这里,我们将输入的数字添加到总和中
总和+=n;
//并添加到列表中,以跟踪我们添加了多少
编号。添加(n);
}
//最后,我们要确保我们要求和的数字超过0,并写出它们的平均值
Console.WriteLine(“平均:”+(numbers.Count>0?sum/numbers.Count:0));
}
}
}
为了给这个问题提供一个详细的例子,这里有一个程序,它将平均标准输入中列出的数字,每行一个。它还显示了如何将给定的数字添加到列表中
using System;
using System.Collections.Generic;
namespace AverageNumbers
{
class MainClass
{
public static void Main (string[] args)
{
// Here is the list of numbers
List<int> numbers = new List<int>();
// Here are two variables to keep track
// of the input number (n), and the sum total (sum)
int n, sum = 0;
// This while loop waits for user input and converts
// that input to an integer
while (int.TryParse(Console.ReadLine(), out n))
{
// Here we add the entered number to the sum
sum += n;
// And to the list to track how many we've added
numbers.Add(n);
}
// Finally we make sure that we have more than 0 numbers that we're summing and write out their average
Console.WriteLine("Average: " + (numbers.Count > 0? sum / numbers.Count : 0));
}
}
}
使用系统;
使用System.Collections.Generic;
命名空间平均数
{
类主类
{
公共静态void Main(字符串[]args)
{
//这是数字清单
列表编号=新列表();
//这里有两个要跟踪的变量
//输入数(n)和总和(sum)的
int n,和=0;
//此while循环等待用户输入并转换
//将该输入转换为整数
while(int.TryParse(Console.ReadLine(),out n))
{
//在这里,我们将输入的数字添加到总和中
总和+=n;
//并添加到列表中,以跟踪我们添加了多少
编号。添加(n);
}
//最后,我们要确保我们要求和的数字超过0,并写出它们的平均值
Console.WriteLine(“平均:”+(numbers.Count>0?sum/numbers.Count:0));
}
}
}
您想在列表中添加多个项目,还是只有一行输入?@rivanov multiple items,我想要一个小控制台应用程序来收集数字列表,然后计算平均值。请参阅下面的我的答案@DovahDo您想在列表中添加多个项目,还是只有一行输入?@rivanov multiple items,我需要一个小控制台应用程序来收集一个数字列表,然后计算平均值。请参阅下面的回答@DovahIm获得Syntax错误,因为“)预期“:/Im获得Syntax错误,因为“)预期”:/谢谢你的帮助!我真的很感谢你抽出时间来!@Dovah,没问题,如果有些台词没有意义就提问。谢谢你的帮助!我真的很感谢你抽出时间来!@Dovah,没问题,如果有些台词没有意义就提问。