简体   繁体   English

C#从主类调用一个带参数的方法

[英]C# Calling a method from the main class with parameters

So I have this program where basically what I'm trying to do is to generate a random array of 20 elements and then shift the values in it a number of places according to a value inputted by the user.所以我有这个程序,基本上我要做的是生成一个包含 20 个元素的随机数组,然后根据用户输入的值将其中的值移动多个位置。 So if the user inputs 5, the array will shift 5 places and the outputted array will have at its first index [0] the value of the index [6] (since index starts from 0 -> 5+1) and at index [1] the value of the original index [7] and so on.因此,如果用户输入 5,数组将移动 5 个位置,并且输出的数组将在其第一个索引 [0] 处具有索引 [6] 的值(因为索引从 0 -> 5+1 开始)和在索引 [ 1] 原始索引的值 [7] 等。

The problem I'm facing lies with the input parameters.我面临的问题在于输入参数。 I'm trying to give in the value given by the user, so if the user inputs 5 then I give the "shiftValueX" to the method as "_shiftValueX" to work with it.我试图给出用户给出的值,所以如果用户输入 5,那么我将“shiftValueX”作为“_shiftValueX”提供给方法以使用它。

Yet below at the "ToString" method where I'm printing out the "shiftPos()" method I'm getting this error, "There is no argument given that corresponds to the required formal parameter".然而,在下面的“ToString”方法中,我正在打印“shiftPos()”方法,我收到此错误,“没有给出与所需形式参数相对应的参数”。 What should I do?我该怎么办? I tried putting parameters to the method calling at the ToString section but nothing works.我尝试将参数放入 ToString 部分调用的方法中,但没有任何效果。 Am I calling the method in a wrong way?我是否以错误的方式调用该方法? How can I fix it?我该如何解决? Thank you so much.非常感谢。

This is the Class "Arrays"这是“数组”类

class Arrays
{
    //constants
    public const int AMOUNT_OF_VALUES = 20;
    public const int MAX_RND_VALUES = 100;

    private int[] array = new int[AMOUNT_OF_VALUES];


    //Random Generator
    private static Random rnd = new Random();

    //Class Constructors
    public Arrays(int[] num)
    {
        for (int i = 0; i < array.Length; i++)
        {
            array[i] = num[i];
        }
    }

    public Arrays()
    {
        for (int i = 0; i < array.Length; i++)
        {
            array[i] = rnd.Next(MAX_RND_VALUES);
        }
    }

    //Methods
    public int[] shiftPos(int _shiftValueX)
    {
        int y = 0;
        int[] array2 = new int[AMOUNT_OF_VALUES];

        for (int i = _shiftValueX; i <= (array.Length - 1); i++)
        {
            array2[y] = array[i];
            y++;
        }

        for (int i = 0; i < (array.Length - (_shiftValueX)); i++)
        {
            array2[y] = array[i];
            y++;
        }

        return array2;
    }

    //Override ToString Method
    public override string ToString()
    {
        return "The original array was: " + string.Join(", ", array.Select(v => v.ToString())) + "\n" +
               "The rotated array is: " + string.Join(", ", shiftPos().Select(v => v.ToString()));
                              //This is where I'm getting the error ^

    }
}

This is the "Main Program"这是“主程序”

class Program
{
    //constants
    public const int AMOUNT_OF_VALUES = 20;

    static void Main(string[] args)
    {

        Console.WriteLine("How many places will the values be shifted: ");
        int shiftValueX = int.Parse(Console.ReadLine());

        Arrays myArray = new Arrays();

        myArray.shiftPos(shiftValueX);
        Console.WriteLine(myArray);
    }
}

Right, fact is you're logic is a little off.是的,事实是你的逻辑有点不对。 As MarcoLaser stated before as well when you call your myArray.shiftPos(shiftValueX);正如 MarcoLaser 之前所说的,当你调用myArray.shiftPos(shiftValueX); in your main you use a separate array which you then return.在您的主要内容中,您使用一个单独的数组,然后返回。 This means your array inside of your Arrays class remains unchanged.这意味着Arrays类中的Arrays保持不变。

What you should most likely do is change the array inside of your Arrays instance in the shiftPos method.您最有可能做的是在shiftPos方法中更改Arrays实例中的Arrays You can still return your second array but you should also set the array你仍然可以返回你的第二个数组,但你也应该设置数组

    this.array = array2;
    return array2;

This way you can use the private array stored in your instance inside the toString() override instead of calling the shiftPos in the toString.通过这种方式,您可以在toString()覆盖中使用存储在您的实例中的私有数组,而不是调用 toString 中的 shiftPos。

You also stated you need the original array as well so just store the array2 in a different private variable.您还表示您还需要原始数组,因此只需将array2存储在不同的private变量中。

A different way of approaching this, though a bit weird, is too have shiftPos not actually execute the logic but make it store the user value in the class instance so you can use the user parameter in the toString method.解决这个问题的另一种方法,虽然有点奇怪,但也让 shiftPos 不实际执行逻辑,而是将用户值存储在类实例中,以便您可以在 toString 方法中使用用户参数。 Though this is kind of a strange way of approaching it and I suggest you do the first method.虽然这是接近它的一种奇怪的方式,但我建议您使用第一种方法。

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

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