Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#方法第二个参数可选_C#_Parameters_Optional - Fatal编程技术网

C#方法第二个参数可选

C#方法第二个参数可选,c#,parameters,optional,C#,Parameters,Optional,我是C#的新手 当前不完整代码 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace classObjectMethodBasic { public class Program { static void Main() { Co

我是C#的新手

当前不完整代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace classObjectMethodBasic
{
    public class Program
    {
        static void Main()
        {
            Console.WriteLine("Input a number for first number to do a math on: ");
            int number1 = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("Input a number for second number to do a math on or you need not enter one: ");
            int number2 = Convert.ToInt32(Console.ReadLine());

            int result1 = new int();
            result1 = Math.math(number1, number2);

            Console.WriteLine("Math result: " + result1);
            Console.ReadLine();
        }
    }
    public class Math
    {
        public static int math(int number1, int number2 = 3)
        {
            int result1 = number1 + number2;
            return result1;
        }
    }
}
需要将第二个参数(number2)设置为可选。 在当前代码中,如果我运行它,但没有为int number2输入值(意味着只需点击回车键),程序就会异常退出,这是有意义的。 异常错误:

System.FormatException: 'Input string was not in a correct format.'
如何使程序以第二个参数作为可选参数工作

谢谢,
Jerry

您已经将第二个参数声明为可选参数。此错误是由以下代码行引起的:

 int number2 = Convert.ToInt32(Console.ReadLine());
在调用
Math.Math()
函数之前,您可以修改代码以检查这两个参数,也可以这样传递这两个参数:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace classObjectMethodBasic
{
    public class Program
    {
        static void Main()
        {
            Console.WriteLine("Input a number for first number to do a math on: ");
            string input1 = Console.ReadLine();
            int number1 = 0;
            int.TryParse(input1, out number1);

            Console.WriteLine("Input a number for second number to do a math on or you need not enter one: ");
            string input2 = Console.ReadLine();
            int number2 = 0;
            int.TryParse(input2, out number2);

            int result1 = new int();
            result1 = Math.math(number1, number2);

            Console.WriteLine("Math result: " + result1);
            Console.ReadLine();
        }
    }
    public class Math
    {
        public static int math(int number1, int number2 = 3)
        {
            int result1 = number1 + number2;
            return result1;
        }
    }
}

更改代码以验证接收到的输入总体上会更好。在您的情况下,您可能没有为第二个值输入值,请参见下面的更正

    static void Main()
    {
        Console.WriteLine("Input a number for first number to do a math on: ");
        int number1 = 0;
        string input1 = Console.ReadLine();
        if (!Int32.TryParse(input1, out number1))
        {
            Console.WriteLine("Number 1 was entered incorrectly");
            Console.ReadLine();
            return;
        }


        Console.WriteLine("Input a number for second number to do a math on or you need not enter one: ");
        int number2 = 0;
        string input2 = Console.ReadLine();


        if (input2.Equals(string.Empty))
        {
            Console.WriteLine("Math result: " + Math.math(number1));
        }
        else
        {
            if (!Int32.TryParse(input2, out number2))
            {
                Console.WriteLine("Number 2 was entered incorrectly");
                Console.ReadLine();
                return;
            }
            else
            {
                Console.WriteLine("Math result: " + Math.math(number1, number2));
            }
        }

        Console.ReadLine();
    }

当您在没有任何输入的情况下点击enter时,
Convert.ToInt32(Console.ReadLine())无法将空白输入转换为整数。您应该首先将
Console.ReadLine()
的值存储到某个字符串值,并检查它是否不为Null或为空。如果它为null或空,则应仅使用第一个数字调用该方法,否则尝试将其转换为整数,并使用两个参数调用该方法。当我从Math.Math调用中删除
number2
时,不会出现异常。请包含引发异常的代码好吗?您的异常与可选参数无关。你做得很好。但是,您在未验证的输入上盲目地使用
Convert.ToInt32()
。如果
Console.ReadLine()
返回一个非数字字符串值(包括但不限于空字符串),则会出现异常。您需要保存
ReadLine()
的结果。如果为空,则需要使用默认参数。如果它不是空的,则应使用
Int32.TryParse()
。如果
TryParse()。谢谢如果我从Math.Math调用中删除数字2,它仍然会失败。J H,我假设您为两个输入输入了一个有效的整数,但我的假设显然是错误的。尽管出于调试目的,我个人总是将
读线设置为param之外的值。谢天谢地,您展示了如何处理垃圾用户输入。我通常也会遵循您描述的模式,但对于这段代码,我专注于其他部分,并复制/粘贴了该部分。感谢您提供的有用输入。为了进一步阐明我对特定任务的意图,它不是强制用户为第一个或第二个参数输入有效值,但是如果用户没有输入第二个参数,则使用默认值int“3”,以便代码仍然可以毫无例外地运行。我将调整代码以继续执行string.Empty,但对于非数值的非空值失败。这可能会给出错误的结果。如果用户输入“字母表”和“10”,输出将为10。如果不是数字,则需要向用户报告错误。同意,但op希望得到有关异常的解决方案,而不是完整的重构。感谢您的帮助。如果我没有为第二个参数输入值,则似乎总是使用值“0”,如果我希望使用默认值“3”,该怎么办?