C#委托程序-在以下的程式中标注出的部份为什麽使用if...else语句不会报错,而使用if...else if 就会报错
namespace委托{classProgram{delegatedoubleProcessDelegate(doubleparam1,doubleparam2);sta...
namespace 委托
{
class Program
{
delegate double ProcessDelegate(double param1, double param2);
static double Multiply(double param1, double param2)
{
return param1 * param2;
}
static double Divide(double param1,double param2)
{
return param1 / param2;
}
static void Main(string[] args)
{
ProcessDelegate process;
Console.WriteLine("Enter 2 numbers separated with comma:");
string input = Console.ReadLine();
int commaPos = input.IndexOf(',');
double param1 = Convert.ToDouble(input.Substring(0,commaPos));
double param2 = Convert.ToDouble(input.Substring(commaPos+1,input.Length-commaPos-1));
Console.WriteLine("Enter M to multiply or D to divide:");
input = Console.ReadLine();
if (input == "M")
process = new ProcessDelegate(Multiply);
else //如果使用else if(input=="D") 会提示以下标注出的错误
process = new ProcessDelegate(Divide);
Console.WriteLine("result: {0}",process(param1,param2)); //该句process会报错Use of //unsigned local variable 'process'
Console.ReadKey();
}
}
} 展开
{
class Program
{
delegate double ProcessDelegate(double param1, double param2);
static double Multiply(double param1, double param2)
{
return param1 * param2;
}
static double Divide(double param1,double param2)
{
return param1 / param2;
}
static void Main(string[] args)
{
ProcessDelegate process;
Console.WriteLine("Enter 2 numbers separated with comma:");
string input = Console.ReadLine();
int commaPos = input.IndexOf(',');
double param1 = Convert.ToDouble(input.Substring(0,commaPos));
double param2 = Convert.ToDouble(input.Substring(commaPos+1,input.Length-commaPos-1));
Console.WriteLine("Enter M to multiply or D to divide:");
input = Console.ReadLine();
if (input == "M")
process = new ProcessDelegate(Multiply);
else //如果使用else if(input=="D") 会提示以下标注出的错误
process = new ProcessDelegate(Divide);
Console.WriteLine("result: {0}",process(param1,param2)); //该句process会报错Use of //unsigned local variable 'process'
Console.ReadKey();
}
}
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询