Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 有没有办法使整型变量等于(=)数组中存储的所有数字?_C#_Arrays_.net_Console_Integer - Fatal编程技术网

C# 有没有办法使整型变量等于(=)数组中存储的所有数字?

C# 有没有办法使整型变量等于(=)数组中存储的所有数字?,c#,arrays,.net,console,integer,C#,Arrays,.net,Console,Integer,好吧,我对编码还不熟悉,我刚在这里开了一个帐户,想得到一些帮助。 我的任务是编写一个应用程序,该应用程序将在控制台中打印0-100之间的数字。但不是“32、44和77”。 我知道我可以做很多If语句,但我的问题是我是否可以将这三个数字存储在一个数组中,然后说: int numZero = 0; int[] num = { 32, 44, 77 }; while (numZero <= 100) {

好吧,我对编码还不熟悉,我刚在这里开了一个帐户,想得到一些帮助。 我的任务是编写一个应用程序,该应用程序将在控制台中打印0-100之间的数字。但不是“32、44和77”。 我知道我可以做很多If语句,但我的问题是我是否可以将这三个数字存储在一个数组中,然后说:

        int numZero = 0;
        int[] num = { 32, 44, 77 };
        
        while (numZero <= 100)
        {
            Console.WriteLine(numZero);
            numZero++;

         // I know the num needs to have a value assigned. But can I make the "numZero" go through the three numbers in num?
           
            if (numZero == num [])  
            {
                numZero++;
            }
int numZero=0;
int[]num={32,44,77};

while(numZero您可以使用从0到100的for循环,并在循环中测试数组中的索引是否不打印或是否要打印

您还可以使用Linq扩展方法代替
Array.Exists()

这样,您将有3行代码:

  • 为了
  • 如果!包含
  • 打印(如果包含,则不执行任何操作)
因此,您可以尝试以下方法:

using System.Linq;

int[] num = { 32, 44, 77 };

for ( int index = 0; index <= 100; index++ )
  if ( !num.Contains(index) )
    Console.WriteLine(index);
使用避免了操纵索引和增量,因此代码更干净、更安全


注意:如果不包括100,除非明确指示您使用数组,否则更改
,我将使用for循环,并使用System.Linq;=>num.Contains(numZero);显式地将循环变量与32、44和47进行比较;谢谢,这在过去帮助了我!我现在带着微笑回顾这一点。
foreach ( int index in Enumerable.Range(0, 101) )
  if ( !num.Contains(index) )
    Console.WriteLine(index);