![](/img/trans.png)
[英]Can I make a function in C# take an optional parameter or do I need to overload it?
[英]How do I make a method parameter optional with user input? (C#)
我在C#中有一个名为“运算符”的对象,该对象具有一种从用户获取两个数字输入并将它们加在一起的方法。 但是,我想使第二个参数(第二个输入)为可选,以便如果用户未输入第二个数字,则默认值为“ 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;
}
}
更新:谢谢大家的回答! 由于有多种建议,我认为我现在可以正常使用了。 非常感谢您的帮助!
如果您省略输入值,则输入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"
这也将起作用。
问题是您要同时使用两个参数来调用方法。 您应该检查是否传递第二个参数。 如下所示:
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();
}
就像是:
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;
}
}
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();
}
}
这个怎么样:
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();
}
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.