简体   繁体   English

如何使方法接收两种不同的数据类型并只返回其中一种C#?

[英]How to make a method receive two different data types and return only one of them C#?

I've started learning C# by myself this week, and I might have trouble understanding a few concepts so sorry for any difficulty understanding me. 我本周开始自己学习C#,我可能无法理解一些概念,所以很难理解我。 I want to create a method which receives int[] and int, but returns only int[]. 我想创建一个接收int []和int的方法,但只返回int []。 I received two errors. 我收到了两个错误。

For the main method: 对于主要方法:

Cannot implicitly convert type 'int[]' to 'int' 无法将类型'int []'隐式转换为'int'

    static void Main(string[] args)
    {
        int[] Num = new int[5];
        int i;
        for (i = 0; i < 5; i++)
            Num[i] = NumGen(Num, i);
    }

The created method: 创建的方法:

Cannot implicitly convert type 'int' to 'int[]' 无法将类型'int'隐式转换为'int []'

  static int[] NumGen(int[] Num, int i)
    {
        Random rndm = new Random();
        Num[i] = rndm.Next(100) + 1;
        return Num[i];

     }

I want to understand what I've done wrong and how to avoid this mistake in the future. 我想了解我做错了什么,以及如何在将来避免这个错误。 Thank you for the answers in advance! 提前谢谢你的答案!

EDIT: Thanks everyone, your suggestions solved the problem. 编辑:谢谢大家,您的建议解决了问题。 :) :)

Just the return type of NumGen - should be int : 只是NumGen的返回类型 - 应该是int

static int NumGen(int[] Num, int i)
{
    Random rndm = new Random();
    Num[i] = rndm.Next(100) + 1;
    return Num[i];
}

BUT: your code has a serious bug. 但是:你的代码有一个严重的错误。 new Random() like that will produce the same value every time, due to how it is seeded via the clock. 像这样的new Random()每次都会产生相同的值,因为它是通过时钟播种的。 A better approach would be: 更好的方法是:

static int NumGen(Random rndm, int[] Num, int i)
{
    Num[i] = rndm.Next(100) + 1;
    return Num[i];
}
static void Main(string[] args)
{
    int[] Num = new int[5];
    Random rand = new Random();
    for (int i = 0; i < 5; i++)
        Num[i] = NumGen(rand, Num, i);
}

Or more cleanly: 或者更干净:

static int Randomize(Random rndm)
{
    return rndm.Next(100) + 1;
}
static void Main(string[] args)
{
    int[] Num = new int[5];
    Random rand = new Random();
    for (int i = 0; i < 5; i++) Num[i] = Randomize(rand);
}

You're declaring a method that returns an array of int ( int[] NumGen() ), while assigning its return value to an int ( Num[i] = NumGen() ) and only returning a single int from it ( return Num[i]; ). 你声明一个方法返回一个int( int[] NumGen() )数组,同时将它的返回值Num[i] = NumGen()一个int( Num[i] = NumGen()并且只返回一个int( return Num[i]; )。

If you can explain what this code is expected to do, perhaps we can suggest what this code should look like. 如果您可以解释这些代码应该做什么,也许我们可以建议这些代码应该是什么样子。

You can probably replace it all with: 你可以用以下内容替换它:

var random = new Random();
int[] randomNumbers = Enumerable.Range(0, 5)
                                .Select(i => random.Next(100) + 1)
                                .ToArray();

But that would miss the point of learning about arrays and their syntax . 但这将错过了解数组及其语法的重点

Num[i] is an int, not int[]. Num [i]是一个int,而不是int []。 Because it is an item from the int array and any element in that array is int. 因为它是int数组中的项,并且该数组中的任何元素都是int。 So replace first line: 所以替换第一行:

static int NumGen(int[] Num, int i)
    {
        Random rndm = new Random();
        Num[i] = rndm.Next(100) + 1;
        return Num[i];

     }

you want your function to return int (a number) not int[] (an array of numbers) 你希望你的函数返回int(数字)而不是int [](数组数组)

change to 改成

static int NumGen(int[] Num, int i)

and it should be ok ;) 它应该没问题;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM