【问题标题】:How do I make a method parameter optional with user input? (C#)如何通过用户输入使方法参数成为可选参数? (C#)
【发布时间】:2019-03-08 00:56:50
【问题描述】:

我在 C# 中有一个名为“operator”的对象,它的方法从用户那里获取两个数字输入并将它们相加。但是,我想将第二个参数(第二个输入)设为可选,以便在用户不输入第二个数字时默认为“4”。

我知道出了点问题,因为如果用户只输入一个数字并在提示输入第二个数字时按回车,它只会结束程序而不是使用默认值。

这个解决方案可能很明显,但它让我望而却步。如果有人能查看我的代码并了解我缺少什么,我将不胜感激。

非常感谢!

程序代码:

class Program
{
    static void Main(string[] args)
    {
        Operator operatorObject = new Operator();
        Console.WriteLine("Pick a number:");
        int userValue = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("Pick another number--optional");
        int userValue2 = Convert.ToInt32(Console.ReadLine());

        int result = operatorObject.operate(userValue, userValue2);

        Console.WriteLine(result);
        Console.ReadLine();
    }
}

类代码:

public class Operator
{
    public int operate(int data, int input=4)
    {
        return data + input;
    }           
}

更新:感谢大家的回答!由于各种建议,我想我现在已经开始工作了。非常感谢您的帮助!

【问题讨论】:

  • 你确定吗? userValue+userValue2 ı 事物 (userValue,UserValue2);
  • 谢谢你,snn。你对那个错误是正确的。我修复了它,但是当没有输入第二个输入时,它仍然不允许默认工作。不过,它确实解决了在输入两者时添加输入的问题。
  • 检查 Console.ReadLine() 是否是一个数字...您将其转换为 int 而无需任何检查。
  • Convert.ToInt32 无法将空字符串转换为数字。当您处理期望将数字作为输入的用户输入时,请始终使用 Int32.TryParse
  • @FreddieMercury 你已经问了 6 个问题,但从未接受过一个。是时候阅读How does accepting an answer works

标签: c# class methods parameters


【解决方案1】:

如果您省略输入值,输入 Console.ReadLine 将返回肯定无法转换为整数的空字符串。

因此,为了使参数能够被省略,您需要指出如果用户输入了任何内容:

int userValue2, userValue2;
int result;
Console.WriteLine("Pick a number:");
if(!int.TryParse(Console.ReadLine(), out userValue))
    throw new ArgumentException("no valid number");

Console.WriteLine("Pick another number--optional");
if(int.TryParse(Console.ReadLine(), out userValue2)
    result = operatorObject.operate(userValue, userValue2);
else
    result = operator.operate(userValue);

int.TryParse 尝试解析用户提供的输入,如果解析失败将返回false。因此,如果用户键入完全不同的内容,例如 "MyString",这也有效。

【讨论】:

  • 谢谢你!这正是我所需要的
【解决方案2】:

问题是您正在使用两个参数调用您的方法。您应该检查是否传递第二个参数。如下所示:

public static void Main()
{
    Operator operatorObject = new Operator();
    Console.WriteLine("Pick a number:");
    int userValue = Convert.ToInt32(Console.ReadLine());
    Console.WriteLine("Pick another number--optional");

    int userValue2;
    int result;
    if(int.TryParse(Console.ReadLine(), out userValue2))
    {
         result = operatorObject.operate(userValue,userValue2);
    } 
    else 
    {
         result = operatorObject.operate(userValue);
    }

    Console.WriteLine(result);
    Console.ReadLine();
}

【讨论】:

  • 这有什么帮助? userValue2总是int。另外,你知道可选参数吗?
  • 不输入就不是int。我要说的要点是,如果您想对参数使用默认值,则不要为其传递值。
  • 我编辑了这个例子来展示整个主要方法来澄清
【解决方案3】:
class Program
{
    static void Main(string[] args)
    {
        Operator operatorObject = new Operator();
        Console.WriteLine("Pick a number:");
        int userValue = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("Pick another number--optional");
        var userValue2IsValid = int.TryParse(Console.ReadLine(), out int userValue2);

        int result = 0;
        if (userValue2IsValid) {
            result = operatorObject.operate(userValue, userValue2);
        }
        else {
            result = operatorObject.operate(userValue);
        }

        Console.WriteLine(result);
        Console.ReadLine();
    }
}

【讨论】:

    【解决方案4】:

    类似:

        static void Main(string[] args)
    {
        Operator operatorObject = new Operator();
        Console.WriteLine("Pick a number:");
        var val1 = Console.ReadLine();
        int userValue = 0;
        if (val1 != null && val1.Length > 0)
        {
            userValue = Convert.ToInt32(val1);
        }
        Console.WriteLine("Pick another number--optional");
        var val2 = Console.ReadLine();
        int userValue = 0;
        int userValue2 = 0;
        if (val2 != null && val2.Length > 0)
        {
            userValue2 = Convert.ToInt32(val2);
        }
    
    
        int result = operatorObject.operate(userValue, userValue2);
    
        Console.WriteLine(result);
        Console.ReadLine();
    }
    public class Operator
    {
        public int operate(int data, int input = 4)
        {
            return data + input;
        }
    }
    

    【讨论】:

    • 取决于您可以使用的 .NET 版本?检查空值,但我尽量保持简单
    • 如果用户在最终的 Console.ReadLine 中没有输入任何数字而按下回车键,这也会崩溃
    • 我想当你刚敲入时,它会给出 "" 作为值,然后你尝试将其转换为 int(提示 Tryparse 是你最好的朋友......)
    • 为什么会是@Steve?
    • 现在好多了,但是,如果用户输入“ABC”而不是数字,您仍然毫无防备。没有出路。当您处理期望获得数字的用户输入时,您需要使用 TryParse 方法
    【解决方案5】:

    这个怎么样:

    class Program
    {
        static void Main(string[] args)
        {
            Operator operatorObject = new Operator();
            Console.WriteLine("Pick a number:");
    
            int result = 0;
    
            int userValue;
            if (int.TryParse(Console.ReadLine(), out userValue))
            {
                Console.WriteLine("Pick another number--optional");
                int userValue2;
                if (int.TryParse(Console.ReadLine(), out userValue2))
                {
                    result = operatorObject.operate(userValue, userValue2);
                }
                else
                {
                    result = operatorObject.operate(userValue);
                }
            }
            else
            {
                Main(null);
            }
    
            Console.WriteLine(result);
            Console.ReadLine();
        }
    
      ...
    

    }

    【讨论】:

    • 谢谢你,杰夫。你对那个错误是正确的。我更改了它,但是当没有输入第二个输入时,它仍然不允许默认工作。不过,它确实解决了在输入两者时添加输入的问题。
    • 谢谢你,杰夫!太棒了。因为我还没有使用 Parse,所以它比我目前使用的要先进一点,但它达到了它的目的并帮助我理解!再次感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-09
    • 2012-04-11
    • 1970-01-01
    • 2011-03-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多