Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.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#_Oop - Fatal编程技术网

C# 通过传递函数获取数组中的输入值

C# 通过传递函数获取数组中的输入值,c#,oop,C#,Oop,我在获取数组中的输入值时遇到一些问题。我们需要为“方法”创建一个函数。让我进一步解释一下 以下是我的全部代码块: using System; class LabExer { static void arrayVal(string[] arr) { for(int i = 0; i < arr.Length; i++) { Console.Write(arr[i] + ": "); arr[

我在获取数组中的输入值时遇到一些问题。我们需要为“方法”创建一个函数。让我进一步解释一下

以下是我的全部代码块:

using System;
class LabExer
{

  static void arrayVal(string[] arr)
  {
    for(int i = 0; i < arr.Length; i++)
        {
            Console.Write(arr[i] + ": ");
            arr[i] = Console.ReadLine();
            
        }
  }

  static void Method1(){
      //Console.Write(letters[i]);
  }
  
  static void Main ()
  {
    string[] letters = {"S", "T", "U", "V", "W", "X", "Y", "Z"};
    int choice = 0;
    while (choice!=5)
    {
        
        Console.WriteLine("Hello! Enter values, then choose your action below:");
        arrayVal(letters);
        
        Console.WriteLine("\n1. Method 1");
        Console.WriteLine("2. Method 2");
        Console.WriteLine("3. Method 3");
        Console.WriteLine("4. Method 4");
        Console.WriteLine("5. Exit");
        
        choice = int.Parse(Console.ReadLine());
        
        switch(choice)
        {
            case 1:
                Method1();
        }
    }
    
  }
}

使用系统;
类LabExer
{
静态void arrayVal(字符串[]arr)
{
对于(int i=0;i
对于方法1:

我试图通过调用
字母[I]
arrayVal
获取用户输入的值,但它给了我一个错误,即上下文中不存在该值。同时,我正试图用一个函数打印出这些值,因为它可能会帮助我弄清楚该怎么做。不幸的是,我还是不能

任何建议都会大有帮助。

这是一个想法:

using System;
class LabExer
{
  private static string[] letters = {"S", "T", "U", "V", "W", "X", "Y", "Z"};

  static void arrayVal(string[] arr)
  {
    for(int i = 0; i < arr.Length; i++)
        {
            Console.Write(arr[i] + ": ");
            arr[i] = Console.ReadLine();
            
        }
  }

  static void Method1(int index){
      Console.Write(letters[index]);
  }
  
  static void Main ()
  {
    int choice = 0;
    while (choice!=5)
    {
        
        Console.WriteLine("Hello! Enter values, then choose your action below:");
        arrayVal(letters);
        
        Console.WriteLine("\n1. Method 1");
        Console.WriteLine("2. Method 2");
        Console.WriteLine("3. Method 3");
        Console.WriteLine("4. Method 4");
        Console.WriteLine("5. Exit");
        
        choice = int.Parse(Console.ReadLine());
        
        switch(choice)
        {
            case 1:
                Method1(0); //your logic to decide which index
                break;
        }
    }
    
  }
}
使用系统;
类LabExer
{
私有静态字符串[]字母={“S”、“T”、“U”、“V”、“W”、“X”、“Y”、“Z”};
静态void arrayVal(字符串[]arr)
{
对于(int i=0;i

由于字母在Main()方法的作用域中,因此出现错误。通过将它放在类级别,您可以从类中的任何方法访问它。

尝试以下操作:

 static void Main(string[] args)
{
    string[] letters = { "S", "T", "U", "V", "W", "X", "Y", "Z" };
    int choice = 0;

    while (choice != 5)
    {
        Console.WriteLine("Hello! Enter values, then choose your action below:");
        arrayVal(letters);

        // print your array with new vales
        for (int i = 0; i < letters.Length; i++)
        {
            Console.WriteLine($"{i + 1}. {letters[i]}");
        }

        choice = int.Parse(Console.ReadLine());

        // to take a single value of array
        string indexValue = letters[choice];

        switch (choice)
        {
            //remind that the array start in position zero (0)
            case 1:    
                Method1();
                break;

            default:
                Console.WriteLine("Invalid Option");
                break;
        }
    }
static void Method1(int i, string[] letters)
{
    Console.Write(letters[i]);
}

// and the call (in the Main)
 case 1:    
     Method1(choice, letters);
     break;

这应该是您的
方法1
-

静态无效方法1(字符串[]个字母)
{
for(int i=0;i
这就是你所说的-

开关(选项)
{
案例1:
方法1(信件);
打破
}
这将打印通过
arrayVal
方法存储的所有用户输入

如果要打印任何特定输入,则-

静态无效方法1(字符串[]个字母,整数索引)
{
控制台。写入(字母[索引]);
Console.WriteLine(“\n\n”);//下一次迭代的新行,没有任何功能
}
并称之为

开关(选项)
{
案例1:
Method1(字母,6);//传递所需的索引作为第二个参数
打破
}
希望对你有用

编辑:
由于您已经将
letters
array作为参数传递给
arrayVal
方法,因此我不建议将
letters
移出
Main
方法。但是你当然可以这样做,使它成为这个类的私有字段。在这种情况下,
arrayVal
Method1
都不需要在签名中使用
string[]
参数。

您需要将数组声明为静态字段,位于Main之外。我不太熟悉如何使用它。我很抱歉。如何实现?正如您将数组传递给
arrayVal
以便它可以访问数组一样,您还需要访问
Method1
。要么将数组作为参数传递给该方法,要么将数组声明为类字段,以便该类的所有方法都可以访问该数组。@insane_developer不需要静态变量,只需参数化该方法即可。静态变量是有状态的,可能会造成很大的危害。@N.Dogac那么您能在静态方法中引用实例字段吗?当然,您可以将所有这些更改为非静态,但实际上,它必须是静态的。我尝试打印
字母[i]
,但它什么也不打印。或者我在打印时做错了什么?您必须向该方法添加一个参数,该参数将是数组
static void Method1(int I){…}的索引,然后使用所需的任何索引调用它。我的回答是告诉你如何放置静态场。我没有运行你的代码。请阅读此文以便理解作用域:我真的不知道为什么,但它仍然说:
方法1中当前上下文中不存在名称“字母”。哦,这是因为字母只是主上下文,而不是方法1。我忘了这个,但现在我编辑代码并更新了。两种解析方法:=>在Method1中传递字母,如Method1(int-choice,string[]字母)或=>在类程序中声明字母,如global