【问题标题】:Returning Three Values from a Method从方法返回三个值
【发布时间】:2014-06-26 17:33:42
【问题描述】:

我正在研究这个解决方案,并尝试返回 3 个不同的 userInput 对 Main 的问题。这是我目前拥有的。

public static string GetInput()
{
    //declared variables
    string userInput;
    string outputTitle;

    Console.Write("Enter your name: ");
    userInput= Console.ReadLine();

    Console.Write("Enter your age: ");
    userInput = Console.ReadLine();

    Console.Write("Enter the Gas Mileage: ");
    userInput = Console.ReadLine();

    return userInput;


}

我设置它的方式从来没有通过(“输入你的名字”);题。有什么更好的方法来解决这个问题?

userName = InputUtilities.GetInput(userName); //错误 2 方法 'GetInput' 没有重载需要 1 个参数

Console.Write("你的名字是:{0}", userName);

【问题讨论】:

  • 你希望这个方法返回什么?
  • 此方法与 Main 属于不同的类。我想获取名称并将其返回到 Main 以显示。然后是年龄,然后是油耗。

标签: c# class return


【解决方案1】:

使用一个类来定义你想要捕获的输入,然后返回它。

    public class UserDetails
    {
        public string Name {get;set;}
        public string Age {get;set;}
        public string Mileage {get;set;}
    }

    public static UserDetails GetInput()
    {
        //declared variables
        //string userInput;
        //string outputTitle;
        var userD = new UserDetails();

        Console.Write("Enter your name: ");
        userD.Name = Console.ReadLine();

        Console.Write("Enter your age: ");
        userD.Age = Console.ReadLine();

        Console.Write("Enter the Gas Mileage: ");
        userD.Mileage = Console.ReadLine();

        return userD;
    }

编辑:使用 out 参数的示例。

void Main()
{
    string name, age, mileage;

    GetInput(out name, out age, out mileage);

    //use name, age and mileage here.
}

public static void GetInput(out string pName, out string pAge, out string pMileage)
{
    Console.Write("Enter your name: ");
    pName = Console.ReadLine();

    Console.Write("Enter your age: ");
    pAge = Console.ReadLine();

    Console.Write("Enter the Gas Mileage: ");
    pMileage = Console.ReadLine();
}

【讨论】:

  • 谢谢你给我看这个!我不确定我是否可以为这个项目创建另一个类。如果允许我创建另一个类,这个想法很有效。
  • 也许我叫错了 InputUtilities.GetInput(out string userName); Console.Write("你的名字是:{0}", userName);
  • 请显示您的代码和实际的错误信息。
  • 对不起,我是这个论坛的新手。我在这里输入代码吗?我的意思是在这个评论区?
  • 编辑您的问题并添加您遇到问题的代码。
【解决方案2】:

如果要返回 3 个值,则必须有 3 个位置来放置它们:

public static List<String> GetInput()
{
    //declared variables
    string userInput;
    List<String> userInputs = new List<String>();

    Console.Write("Enter your name: ");
    userInput= Console.ReadLine();
    userInputs.Add(userInput);

    Console.Write("Enter your age: ");
    userInput = Console.ReadLine();
    userInputs.Add(userInput);

    Console.Write("Enter the Gas Mileage: ");
    userInput = Console.ReadLine();
    userInputs.Add(userInput);

    return userInput;
}

【讨论】:

    【解决方案3】:

    您将用户为每个问题输入的值分配给同一个变量。每次写入都会覆盖先前的值,这意味着您应该只得到最后一个问题的答案(“输入汽油里程”)。有几种通用的方法可以解决这个问题:

    1) 将每个连续的答案附加到同一个字符串(不推荐)。

    2) 创建一些对象来存储所有答案(类或列表)。

    3) 使用 out 参数。

    由于其他两个答案已经解释过了,这里是out参数:

    public static void GetInput(out string pName, out string pAge, out string pMileage)
    {
        // do stuff here ....
    }
    

    【讨论】:

    • 我想这就是我要找的。谢谢。我要试试这个。
    • 这会导致错误。当前上下文中不存在名称“pName”。当我将它拉到 Main 时会发生这种情况。我是否声明了 pName、pAge、pMileage?
    • 你怎么称呼它?您需要在main中声明它们,并在调用GetInput()时将它们用作out参数
    • 用户名 = InputUtilities.GetInput(pName); Console.Write("你的名字是:{0}", userName);
    • 如果我在 Main 中声明 pName、pAge、pMileage,则错误是方法没有重载。接受 1 个参数。
    【解决方案4】:

    或者

    public static object GetInput()
    {
        Console.Write("Enter your name: ");
        var _name = Console.ReadLine();
    
        Console.Write("Enter your age: ");
        var _age  = Console.ReadLine();
    
        Console.Write("Enter the Gas Mileage: ");
        vae _mileage = Console.ReadLine();
    
        return new { name = _name, age = _age, mileage = _mileage };
    }
    

    或者

    public static dynamic GetInput()
    {
        //declared variables
        dynamic userinputs;
    
        Console.Write("Enter your name: ");
        var _name = Console.ReadLine();
    
        Console.Write("Enter your age: ");
        var _age  = Console.ReadLine();
    
        Console.Write("Enter the Gas Mileage: ");
        vae _mileage = Console.ReadLine();
    
        userinputs.name = _name;
        userinputs.age = _age;
        userinputs.mileage = _mileage;
    
        return userinputs;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-12
      • 2012-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多