【问题标题】:Using char variables in other methods *edited*在其他方法中使用 char 变量*已编辑*
【发布时间】:2014-03-28 06:35:57
【问题描述】:

我正在尝试从用户那里获取一串字符并将它们转换为电话号码。

我们还没有在课堂上讨论过数组,所以我不想在这个程序中使用类似的东西。我对他们也不太了解。我们复习了传递参考,但我并没有真正理解它。

我的问题是如何在其他方法中使用我的 char 变量?我尝试将变量放在类下,但这也不起作用。我收到的最常见的错误消息是:

非静态字段、方法需要对象引用。

这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Damian_CIS170B_Lab4
{
    class Program
    {
        //char char1;

       static void Main(string[] args)
        {
            Console.WriteLine("****Phone Dialing Program****\n");

            char char1;

                //char2, char3, char4, char5, char6, char7;

            GetInput(ref char1);
            ProcessInput();
            ToDigit();
            ShowResults();

            Console.Read();
        }

        static void GetInput(ref char1)
        {
            Console.WriteLine("Enter your first character:");
            Console.ReadLine() = char.Parse(char1);

           /* Console.WriteLine("Enter your second character:");
              Console.WriteLine("Enter your third character:");
              Console.WriteLine("Enter your fourth character:");
              Console.WriteLine("Enter your fifth character:");
              Console.WriteLine("Enter your sixth character:");
              Console.WriteLine("Enter your seventh character:"); */
        }    

        static void ProcessInput()
        {
        }

        static void ToDigit()
        {
        }

        static void ShowResults()
        {
        }
    }
}

所以我选择了这个

            Console.WriteLine("Enter your first character:");
            char1 = Console.ReadKey().KeyChar;

2/25/14 当我使用它时,它只会输入第一个字符。我可以输入 2 到 7,但是当我尝试让它写下所有字符时,它只写第一个字符,这是为什么呢?我怎样才能解决这个问题? 新代码:

 static void Main(string[] args)
    {
        Console.WriteLine("****Phone Dialing Program****\n");

        char char1 = default(char);
        char char2 = default(char);
        char char3 = default(char);
        char char4 = default(char);
        char char5 = default(char);
        char char6 = default(char);
        char char7 = default(char);


        GetInput(ref char1, char2, char3, char4, char5, char6, char7);
        ProcessInput(ref char1, char2, char3, char4, char5, char6, char7);
        //ToDigit(ref char1, char2, char3, char4, char5, char6, char7);
        ShowResults();


        Console.Read();
    }

    static void GetInput(ref char char1, char char2, char char3, char char4, char char5, char char6, char char7)
    {
        Console.WriteLine("Enter your first character:");
        char1 = Console.ReadKey().KeyChar;

        Console.WriteLine("\nEnter your second character:");
        char2 = Console.ReadKey().KeyChar;

        Console.WriteLine("\nEnter your third character:");
        char3 = Console.ReadKey().KeyChar;

        Console.WriteLine("\nEnter your fourth character:");
        char4 = Console.ReadKey().KeyChar;

        Console.WriteLine("\nEnter your fifth character:");
        char5 = Console.ReadKey().KeyChar;

        Console.WriteLine("\nEnter your sixth character:");
        char6 = Console.ReadKey().KeyChar;

        Console.WriteLine("\nEnter your seventh character:");
        char7 = Console.ReadKey().KeyChar;

        ToDigit(ref char1, char2, char3, char4, char5, char6, char7);

    }

    static void ProcessInput(ref char char1, char char2, char char3, char char4, char char5, char char6, char char7)
    {
        char[] chars = { char1, char2, char3, char4, char5, char6, char7 };
        string enteredChars = new string(chars);

        //This is me just trying to see if it is working... its not :(
        Console.WriteLine("This is what you entered: {0}", enteredChars);

    }

    static void ToDigit(ref char char1, char char2, char char3, char char4, char char5, char char6, char char7)
    {
        switch(char1)
        {
            case 'A':
            case 'a':
            case 'B':
            case 'b':
            case 'C':
            case 'c':
            case '2': Console.WriteLine("\n2");
                break;
            default: Console.WriteLine("\n");
                break;
        }

    }

    static void ShowResults()
    {
    }

【问题讨论】:

  • 好吧,你注释掉的字段是一个 instance 变量(即它与你的类型的实例相关联),但你所有的方法都是 static方法 - 你永远不会真正创建 Program 的实例。
  • 一定要用ref吗?没有它会更容易,更清洁
  • 感谢 selman22 和 NewHire!终于编译成功了!

标签: c# variables methods console-application pass-by-reference


【解决方案1】:

你的方法应该是这样的:

static void GetInput(ref char char1)
{
    Console.WriteLine("Enter your first character:");
    char1 = Console.ReadKey().KeyChar;
}

您在参数中缺少数据类型。

然后这样称呼它:

char char1 = default(char);
GetInput(ref char1);

在您的方法中,您不需要char.Parse,相反,我相信您正在尝试从用户那里获取字符输入,应该这样做:

char1 = Console.ReadKey().KeyChar;

您还可以修改您的方法以返回char,而不是发送带有ref 关键字的参数。

【讨论】:

    【解决方案2】:

    通过引用传递允许您的方法更改值并将该值反映在方法之外,但是它仍然需要有一个值进入。您描述的错误是因为您尚未初始化 char1。有两种简单的方法可以解决这个问题,第一种是初始化 char1:char char1 = 'x';。另一种是从通过引用传递更改为作为输出参数传递。出于您的目的,refoutput 之间的区别在于 ref 期望值在调用方法之前被初始化,而 output 期望它在被调用的方法中的某个位置被初始化。

    【讨论】:

      【解决方案3】:

      你应该在这里指定参数类型,这不会编译:

      static void GetInput(ref char1)
      

      应该是这样的:

      static void GetInput(ref char char1)
      

      那么在你将一个变量作为 ref 参数传递给一个函数之前,你应该初始化它,这意味着你应该给它一个默认值:

      char char1 = default(char);
      

      然后你可以将它传递给你的函数:

      GetInput(ref char1);
      

      如果您想在其他函数中使用此变量,而不是使用ref,我会将我的方法定义为扩展方法并返回结果char,如下所示:

      public static class MyCharExtensions
      {
          public static char GetInput()
          {
              return Console.ReadKey().KeyChar;
          }
          public static char ProcessInput(this char source)
          {
              // do your work with char and return it
          }
          public static char ToDigit(this char source)
          {
              // do your work with char and return it
          }
          public static void ShowResults(this char source)
          {
      
          }
      }
      

      并像这样使用它们:

      MyCharExtensions.GetInput().ProcessInput().ShowResults();
      

      【讨论】:

        【解决方案4】:

        你必须使用ref吗?如果没有,只需让 GetInput 返回 char 而不是 void。这可能是你的开始:

        static void Main(string[] args)
            {
                Console.WriteLine("****Phone Dialing Program****\n");
        
                char first = GetInput("first");
                char second = GetInput("second");
                char third = GetInput("third");
                char fourth = GetInput("fourth");
                char fifth = GetInput("fifth");
                char sixth = GetInput("sixth");
                char seventh = GetInput("seventh");
                char eighth = GetInput("eigth");
        
                Console.Write(new string(new []{first,second,third,fourth,fifth,sixth,seventh,eighth}));
        
                Console.Read();
            }
        
            static char GetInput(string count)
            {
                Console.WriteLine("Enter your " + count + " character:");
                return Console.ReadLine()[0];
            }    
        

        【讨论】:

        • 我无法让它工作......我同意它会让我的代码看起来更干净。
        猜你喜欢
        • 2021-01-24
        • 1970-01-01
        • 2016-05-09
        • 1970-01-01
        • 2021-09-24
        • 1970-01-01
        • 1970-01-01
        • 2013-03-26
        相关资源
        最近更新 更多