使用数组的for循环范围
Range in for loop using Array
我是 C# 的新手,希望用户能够输入 1 到 25 之间的 5 个数字。我遇到的问题是我不希望用户输入超过 25 的数字或小于 1 的数字。
这也是我的学习任务,我的老师要我们使用数组,所以我不能使用列表。
int[] usernum = new int[4];
for (int i = 0; i < usernum.Length; i++)
{
usernum[i] = Convert.ToInt32(Console.ReadLine());
}
虽然我建议你先学习 if else 循环 https://www.w3schools.com/cs/cs_conditions.asp
如果需要,这里是代码
int[] usernum = new int[4];
for (int i = 0; i < usernum.Length; i++)
{
var result = Console.ReadLine();
int currentResult;
if (!int.TryParse(result, out currentResult))
{
Console.WriteLine("Invalid input - must be a valid integer value");
i--;
continue;
}
if(currentResult < 1 || currentResult > 25)
{
Console.WriteLine("Invalid input - must be between 1 & 25");
i--;
continue;
}
usernum[i] = currentResult;
}
好的,首先,对您的代码进行一些注释:
int[] usernum = new int[4]; // should be: new int[5];
for (int i = 0; i < usernum.Length; i++)
{
usernum[i] = Convert.ToInt32(Console.ReadLine()); // use int.TryParse instead
}
现在,我不想只给你代码,因为这显然应该是一种学习经验。
您需要做,不过是集成一个“验证”周期。这意味着:
- 从用户那里读入字符串
- 尝试将字符串解析为数字
如果失败:回到 1.
- 检查
if
个数 < 1 或 > 25
如果是:回到 1.
- 如果你在这里,你通过了两项检查,可以
设置 usernum[i] = number
- 下一个“我”
显然,在如何扭转和转动支票以及安排同样有效的循环方面存在一些细微的变化。
例如:您可以决定是要检查数字是否在 范围内 还是要检查数字是否在 范围外 [=51] =] 跳或不跳相应的界限...
为什么 int.TryParse
而不是 Convert.ToInt32
?
有一些经验法则可以让您免于严重的头痛:
- “永远不要相信用户输入”
- “不要将异常用于控制流”
在这里使用转换,打破两者。
首先,如果字符串不表示整数值(+-0..9 以外的字符,值 > int.Max 或 < int.Min),Convert.ToInt32 将抛出异常。因此,在使用它时,您相信用户会输入一个有效的整数。不是个好主意。
然后,它抛出意味着:用户(可能只是输入错误)没有提供有效输入的情况正在控制您的错误处理流程。但这个案例一点也不“例外”。事实上,您应该期待它。 int.TryParse
使这成为可能,因为它 returns 你是一个标志(布尔值),通知你转换尝试成功或失败(而不是抛出)。
for-loop
可能不是这个 use-case 的理想解决方案,您需要有条件地增加索引。
这应该可以解决问题:
int[] userNumbers = new int[5];
int i = 0;
while (i < userNumbers.Length)
{
string rawInput = Console.ReadLine();
bool isNumberValid = int.TryParse(rawInput, out int inputNumber); // as suggested by @Fildor
if(isNumberValid && inputNumber >= 1 && inputNumber <= 25) // increment counter only if 1 <= input <= 25
{
userNumbers[i] = inputNumber;
i++;
}
}
我是 C# 的新手,希望用户能够输入 1 到 25 之间的 5 个数字。我遇到的问题是我不希望用户输入超过 25 的数字或小于 1 的数字。
这也是我的学习任务,我的老师要我们使用数组,所以我不能使用列表。
int[] usernum = new int[4];
for (int i = 0; i < usernum.Length; i++)
{
usernum[i] = Convert.ToInt32(Console.ReadLine());
}
虽然我建议你先学习 if else 循环 https://www.w3schools.com/cs/cs_conditions.asp
如果需要,这里是代码
int[] usernum = new int[4];
for (int i = 0; i < usernum.Length; i++)
{
var result = Console.ReadLine();
int currentResult;
if (!int.TryParse(result, out currentResult))
{
Console.WriteLine("Invalid input - must be a valid integer value");
i--;
continue;
}
if(currentResult < 1 || currentResult > 25)
{
Console.WriteLine("Invalid input - must be between 1 & 25");
i--;
continue;
}
usernum[i] = currentResult;
}
好的,首先,对您的代码进行一些注释:
int[] usernum = new int[4]; // should be: new int[5];
for (int i = 0; i < usernum.Length; i++)
{
usernum[i] = Convert.ToInt32(Console.ReadLine()); // use int.TryParse instead
}
现在,我不想只给你代码,因为这显然应该是一种学习经验。
您需要做,不过是集成一个“验证”周期。这意味着:
- 从用户那里读入字符串
- 尝试将字符串解析为数字
如果失败:回到 1. - 检查
if
个数 < 1 或 > 25
如果是:回到 1. - 如果你在这里,你通过了两项检查,可以 设置 usernum[i] = number
- 下一个“我”
显然,在如何扭转和转动支票以及安排同样有效的循环方面存在一些细微的变化。
例如:您可以决定是要检查数字是否在 范围内 还是要检查数字是否在 范围外 [=51] =] 跳或不跳相应的界限...
为什么 int.TryParse
而不是 Convert.ToInt32
?
有一些经验法则可以让您免于严重的头痛:
- “永远不要相信用户输入”
- “不要将异常用于控制流”
在这里使用转换,打破两者。
首先,如果字符串不表示整数值(+-0..9 以外的字符,值 > int.Max 或 < int.Min),Convert.ToInt32 将抛出异常。因此,在使用它时,您相信用户会输入一个有效的整数。不是个好主意。
然后,它抛出意味着:用户(可能只是输入错误)没有提供有效输入的情况正在控制您的错误处理流程。但这个案例一点也不“例外”。事实上,您应该期待它。 int.TryParse
使这成为可能,因为它 returns 你是一个标志(布尔值),通知你转换尝试成功或失败(而不是抛出)。
for-loop
可能不是这个 use-case 的理想解决方案,您需要有条件地增加索引。
这应该可以解决问题:
int[] userNumbers = new int[5];
int i = 0;
while (i < userNumbers.Length)
{
string rawInput = Console.ReadLine();
bool isNumberValid = int.TryParse(rawInput, out int inputNumber); // as suggested by @Fildor
if(isNumberValid && inputNumber >= 1 && inputNumber <= 25) // increment counter only if 1 <= input <= 25
{
userNumbers[i] = inputNumber;
i++;
}
}