繁体   English   中英

C#方法第二个参数可选

[英]C# method second parameter 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()
        {
            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的值(意味着只需按Enter键),程序将退出异常,这是有意义的。 异常错误:

System.FormatException: 'Input string was not in a correct format.'

如何使程序使用第二个参数作为可选项?

谢谢,杰里

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

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

您已将第二个参数声明为可选参数。 错误是由于这行代码:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM