c#变量或数组的数字范围(示例)。1 - 100)

时间:2022-02-21 21:38:44

I'm fairly new to C# and I'm doing a school project, i need to figure out how to get a variable or an array with numbers from 1 to 100 without entering every single number in an array for example int[] numbersArray {1,2,3,4,5,6,7,8,9,10...}; because that takes a long time and doesn't look very efficient.

我对c#相当陌生,我正在做一个学校项目,我需要弄清楚如何获得一个变量或一个数字从1到100的数组,而不输入数组中的每个数字,例如int[] numbersArray {1,2,3,4,5,6,6,7,8,9,10};因为这需要很长时间,而且看起来效率不高。

I'm using C# Visual Studio Express 2010. It would mean alot to me if you could answer this for me. I'm gonna be using it in an if statement like so:

我正在使用c# Visual Studio Express 2010。如果你能帮我回答,对我来说意义重大。我会在if语句中用到它

if(numbersArray.Contains(numbersInput))
{
    Console.WriteLine("numbersInput was a number from 1 to 100")
}

4 个解决方案

#1


22  

You can use Enumerable.Range to create a range of numbers:

您可以使用枚举。创建一系列数字的范围:

int[] arr = Enumerable.Range(1, 100).ToArray();

#2


2  

If you're assignment is just to print a message if the input is within a range you simply can do this:

如果你的任务是打印一条消息如果输入在一个范围内你可以这样做:

if (numbersInput >= 1 && numbersInput <= 100)
    Console.WriteLine("numbersInput was a number from 1 to 100");

But if you really need to create an array with numbers 1..100 you can use a for-loop:

但是如果你真的需要创建一个编号为1的数组。你可以使用for循环:

var numbersArray = new int[100];
for (var i = 1; i <= 100; i++)
    numbersArray[i - 1] = i;

Or simply use a little Linq:

或者简单地使用一些Linq:

var numbersArray = Enumerable.Range(1, 100).ToArray();

#3


1  

you could just use a for loop with the iterator of the loop as the counter:

您可以使用循环的迭代器作为计数器:

int[] numbersArray = new int[100] // initialise array to 100 elements.
for (int i = 1; i <= 100; i++)
{
    numbersArray[i - 1] = i;  // note we are using 0-based indexing to access elements of the array
}

#4


1  

Other way...

另一种方式……

int[] arr = new int[100];
for(int i = 0; i < arr.Length; ++i)
{
    arr[i]=i+1;
}

#1


22  

You can use Enumerable.Range to create a range of numbers:

您可以使用枚举。创建一系列数字的范围:

int[] arr = Enumerable.Range(1, 100).ToArray();

#2


2  

If you're assignment is just to print a message if the input is within a range you simply can do this:

如果你的任务是打印一条消息如果输入在一个范围内你可以这样做:

if (numbersInput >= 1 && numbersInput <= 100)
    Console.WriteLine("numbersInput was a number from 1 to 100");

But if you really need to create an array with numbers 1..100 you can use a for-loop:

但是如果你真的需要创建一个编号为1的数组。你可以使用for循环:

var numbersArray = new int[100];
for (var i = 1; i <= 100; i++)
    numbersArray[i - 1] = i;

Or simply use a little Linq:

或者简单地使用一些Linq:

var numbersArray = Enumerable.Range(1, 100).ToArray();

#3


1  

you could just use a for loop with the iterator of the loop as the counter:

您可以使用循环的迭代器作为计数器:

int[] numbersArray = new int[100] // initialise array to 100 elements.
for (int i = 1; i <= 100; i++)
{
    numbersArray[i - 1] = i;  // note we are using 0-based indexing to access elements of the array
}

#4


1  

Other way...

另一种方式……

int[] arr = new int[100];
for(int i = 0; i < arr.Length; ++i)
{
    arr[i]=i+1;
}