C# 输入数字,第一个决定将有多少
我需要将一些数字从控制台输入放入一个数组,其中第一个数字决定将有多少个(该数字*2)。这些数字最多有两位数。 到目前为止,我的代码仍然无法正常工作C# 输入数字,第一个决定将有多少,c#,console,C#,Console,我需要将一些数字从控制台输入放入一个数组,其中第一个数字决定将有多少个(该数字*2)。这些数字最多有两位数。 到目前为止,我的代码仍然无法正常工作 int quantity = Convert.ToInt32(Console.Read()); int[] arr = new int[100]; int count = 0; while (count < quantity*2
int quantity = Convert.ToInt32(Console.Read());
int[] arr = new int[100];
int count = 0;
while (count < quantity*2)
{
string line = Console.ReadLine();
line = line + ' ';
for (int i = 0; i < line.Length; i++)
{
char c = line[i];
if ((c>='0')&&(c<='9'))
{
char d = line[i + 1];
if ((d >= '0') && (d <= '9'))
{
arr[count] = c * 10 + d;
i++;
}
else
{
arr[count] = c;
}
count++;
}
}
int-quantity=Convert.ToInt32(Console.Read());
int[]arr=新int[100];
整数计数=0;
同时(计数<数量*2)
{
string line=Console.ReadLine();
直线=直线+“”;
for(int i=0;i 如果((c>='0')&&(c='0')&&(d存在两个错误,无法使程序正常工作
// Do not use Console.Read, but ReadLine to wait the end of input from your user
string userInput Console.ReadLine();
// Convert to a number using Int32.TryParse to check if your user really types a number
// and not something that will raise an exception if it is not a number
int quantity;
if(!Int32.TryParse(userInput, out quantity))
{
Console.WriteLine("An integer number is required!");
return;
}
// Dimension you array size large enough to contain the numbers required in the
// following loop (I am not sure if now you require to double the size of the array)
int[] arr = new int[quantity*2];
int count = 0;
while (count < quantity*2)
{
string line = Console.ReadLine();
int inputNumber;
// Again to convert to a number, use Int32.TryParse
if(Int32.TryParse(line, inputNumber))
{
arr[count] = inputNumber;
count++;
}
else
{
Console.WriteLine("An integer number is required!");
}
}
//不要使用Console.Read,而是使用ReadLine等待用户输入的结束
字符串userInput Console.ReadLine();
//使用Int32.TryParse转换为数字,以检查用户是否真的键入了数字
//如果它不是一个数字,就不会引起异常
整数;
如果(!Int32.TryParse(用户输入,输出数量))
{
WriteLine(“需要整数!”);
返回;
}
//维度的数组大小足以包含
//以下循环(我不确定现在是否需要将数组的大小增加一倍)
int[]arr=新int[数量*2];
整数计数=0;
同时(计数<数量*2)
{
string line=Console.ReadLine();
输入整数;
//再次使用Int32.TryParse转换为数字
if(Int32.TryParse(行,输入编号))
{
arr[count]=输入编号;
计数++;
}
其他的
{
WriteLine(“需要整数!”);
}
}
使用允许更好地处理用户输入。无需使用单个字符搜索无效输入。(顺便说一下,您的实际方法将代码限制为不大于两位数的数字)
但是,在这样的上下文中,当您不知道数组的确切大小时,建议使用的数据结构是List
//创建整数列表,不需要大小
List arr=新列表();
整数计数=0;
//不确定现在是否需要将数组的大小增加一倍
同时(计数<数量)
{
string line=Console.ReadLine();
输入整数;
if(Int32.TryParse(行,输入编号))
{
//将输入添加到列表。。。。
arr.Add(输入编号);
计数++;
}
其他的
{
WriteLine(“需要整数!”);
}
}
您可以轻松地使用列表,因为它是一个数组
for(int x = 0; x < arr.Count; x++)
Console.WriteLine(arr[x].ToString());
for(int x=0;x
根据下面的注释进行编辑。如果行可能包含多个数字,则需要对用户输入进行拆分,但这不会影响TryParse检查,您只需要额外的循环和对插入的最大元素数的控制
int[] arr = new int[quantity];
int count = 0;
while (count < quantity)
{
string line = Console.ReadLine();
int inputNumber;
// Split on space and tabs and remove the resulting
// empty elements if there are two space/tabs consecutive.
string[] parts = line.Split(new char[] {' ', '\t'}, StringSplitOptions.RemoveEmptyEntries);
foreach(string aNumber in parts)
{
if(Int32.TryParse(line, inputNumber))
{
arr[count] = inputNumber;
count++;
}
else
{
Console.WriteLine(aNumber.ToString() + " is not an integer number!");
}
if(count >= arr.Length)
break;
}
}
int[]arr=新的int[数量];
整数计数=0;
同时(计数<数量)
{
string line=Console.ReadLine();
输入整数;
//在空格和制表符上拆分并删除结果
//如果有两个空格/制表符连续,则为空元素。
string[]parts=line.Split(新字符[]{'','\t'},StringSplitOptions.RemoveEmptyEntries);
foreach(部分字符串和编号)
{
if(Int32.TryParse(行,输入编号))
{
arr[count]=输入编号;
计数++;
}
其他的
{
WriteLine(aNumber.ToString()+“不是整数!”);
}
如果(计数>=arr.Length)
打破
}
}
我认为这是可行的:
Console.Write("Enter the number to collect (*2):");
int initialQuantity = Convert.ToInt32(Console.ReadLine());
int calculatedQuantity = initialQuantity * 2;
int[] arr = new int[calculatedQuantity];
int count = 0;
while (count < calculatedQuantity)
{
Console.Write("Enter the number:");
string line = Console.ReadLine();
int inputNumber;
if (int.TryParse(line, out inputNumber) && line.Length <= 2)//is valid
arr[count++] = inputNumber;
}
Console.Write(“输入要收集的号码(*2):”;
int initialQuantity=Convert.ToInt32(Console.ReadLine());
int calculatedQuantity=初始数量*2;
int[]arr=新int[calculatedQuantity];
整数计数=0;
同时(计数<计算数量)
{
控制台。写入(“输入号码:”);
string line=Console.ReadLine();
输入整数;
if(int.TryParse(line,out-inputNumber)&&line.Length优先,Console.Read()
返回一个表示单个字符的整数。这会混淆Convert.ToInt32
。请改用Console.ReadLine
。欢迎使用StackOverflow了解您的信息。不需要在标题中包含标记,因为您已经在标记部分中提到了您的问题是什么?但唯一的问题是什么(谈到第一个代码,即带有数组的代码)是我可以在一行中用空格(或制表符)分隔多个数字,因此string.Split()不是一个选项。请帮我解决这个问题好吗?(很抱歉没有提到它,谢谢你的回答)添加了多行部分的检查[谈论最后一个代码]仍然不起作用。当我输入时:“3 1\n 2 1\n 1 2 1”表示,即在最后一行中,1不是整数,2不是整数…[我在Split()中添加了“\n”,因为数字也可以用换行符分隔]请帮助。
Console.Write("Enter the number to collect (*2):");
int initialQuantity = Convert.ToInt32(Console.ReadLine());
int calculatedQuantity = initialQuantity * 2;
int[] arr = new int[calculatedQuantity];
int count = 0;
while (count < calculatedQuantity)
{
Console.Write("Enter the number:");
string line = Console.ReadLine();
int inputNumber;
if (int.TryParse(line, out inputNumber) && line.Length <= 2)//is valid
arr[count++] = inputNumber;
}