[英]How do I make a method parameter optional with user input? (C#)
I have an object called "operator" in C# with a method that takes two number inputs from a user and adds them together. 我在C#中有一个名为“运算符”的对象,该对象具有一种从用户获取两个数字输入并将它们加在一起的方法。 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.
但是,我想使第二个参数(第二个输入)为可选,以便如果用户未输入第二个数字,则默认值为“ 4”。
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!
非常感谢您的帮助!
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. 如果您省略输入值,则输入
Console.ReadLine
将返回空字符串,该字符串肯定不能转换为整数。
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
. int.TryParse
尝试解析用户提供的输入,如果解析失败将返回false
。 So this also works if user types something completely different like "MyString"
. 因此,如果用户键入完全不同的内容(例如
"MyString"
这也将起作用。
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();
}
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;
}
}
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();
}
}
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();
}
...
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.