How do I make a method parameter optional with user input? (C#)

拟墨画扇 提交于 2021-01-29 07:15:16

问题


I have an object called "operator" in C# with a method that takes two number inputs from a user and adds them together. However, I want to make the second parameter (2nd input) optional so that the default is "4" if the user doesn't enter a second number.

I know something's wrong because it just ends the program rather than using the default if the user enters just one number and hits enter when prompted for second input.

This solution is probably very obvious but it's eluding me. I'd appreciate if someone would take a look at my code and see what I'm missing.

Thank you so much!

program code:

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();
    }
}

class code:

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

UPDATE: Thank everyone for your answers! I think I've got it working now, due to a combination of suggestions. Your help is much appreciated!


回答1:


If you omit to enter a value the input Console.ReadLine will return the empty string which surely can´t be converted to an integer.

So in order to enable the parameter to be omitted you need to indicate if the user entered anything at all:

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 tries to parse the input provided by user and if parsing fails will return false. So this also works if user types something completely different like "MyString".




回答2:


The problem is that you are calling your method with both parameters. You should check whether to pass the second parameter. Something like follows:

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();
}



回答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:


Something like:

    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;
    }
}



回答5:


How about this:

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();
    }

  ...

}



来源:https://stackoverflow.com/questions/52616118/how-do-i-make-a-method-parameter-optional-with-user-input-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!