c# 为什么会出现使用未被赋值局部变量 委托 本帖最后由 mozart0127 于 2009-09-19 15:50:45 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 if(h=="*") d=new mydelegate(chen); else if(h=="/") d=new mydelegate(chu);很简单的例子,如果h=='a'的话,你的if(h=="*")和 else if(h=="/")都不成立,所以编译器抱错如果你能确保这两个分之至少有1个成立的话,可以这样: if(h=="*") d=new mydelegate(chen); else //这里不再判断,这样if和else至少有1 个成立 d=new mydelegate(chu);或者这样:d=null;//这样可以通过编译器检查 if(h=="*") d=new mydelegate(chen); else if(h=="/") d=new mydelegate(chu); 还可以这样: if(h=="*") d=new mydelegate(chen); else if(h=="/") d=new mydelegate(chu); else if(h=='+')else if..else ifelse throw new Exception(); class Program { delegate double mydelegate(double a, double b); static double chen(double a, double b) { return a * b; } static double chu(double a, double b) { return a / b; } static void Main(string[] args) { mydelegate d; Console.WriteLine("输入两个数字:"); double a = Convert.ToDouble(Console.ReadLine()); double b = Convert.ToDouble(Console.ReadLine()); Console.WriteLine("请输入运算符"); string h=Console.ReadLine(); if(h=="*") d=new mydelegate(chen); else if(h=="/") d=new mydelegate(chu); //主要在这里了,万一h!=×和/ 下面这句中的d(a,b)就没实例化了 Console.WriteLine("result:{0}",d(a,b) ); Console.ReadKey(); } c#中webbrowser控件如何捕捉ajax调用? 莫名其妙的数据库异常 关于foreach动态生成文本框后焦点的问题 winform 页面显示问题 类设计的概念,请大家帮忙看下... 如何在datagrid列中添加下拉列表 谁有DevPartner Studio Professional Edition? C#:嵌入的语句不能是声明或标记语句 如果用C#写应用工具,会得到多少支持?现在的.net framework的装机量不会太大吧 急救!!关于vs.net2003安装问题。 中文转拼音(个别汉字转换出错) FileDialog问题
d=new mydelegate(chen);
else if(h=="/")
d=new mydelegate(chu);
很简单的例子,如果h=='a'的话,你的if(h=="*")和 else if(h=="/")都不成立,所以编译器抱错如果你能确保这两个分之至少有1个成立的话,可以这样:
if(h=="*")
d=new mydelegate(chen);
else //这里不再判断,这样if和else至少有1 个成立
d=new mydelegate(chu);或者这样:
d=null;//这样可以通过编译器检查
if(h=="*")
d=new mydelegate(chen);
else if(h=="/")
d=new mydelegate(chu);
if(h=="*")
d=new mydelegate(chen);
else if(h=="/")
d=new mydelegate(chu);
else if(h=='+')
else if..
else if
else
throw new Exception();
class Program
{ delegate double mydelegate(double a, double b);
static double chen(double a, double b)
{
return a * b;
}
static double chu(double a, double b)
{
return a / b;
}
static void Main(string[] args)
{
mydelegate d;
Console.WriteLine("输入两个数字:");
double a = Convert.ToDouble(Console.ReadLine());
double b = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("请输入运算符");
string h=Console.ReadLine();
if(h=="*")
d=new mydelegate(chen);
else if(h=="/")
d=new mydelegate(chu);
//主要在这里了,万一h!=×和/ 下面这句中的d(a,b)就没实例化了
Console.WriteLine("result:{0}",d(a,b) );
Console.ReadKey();
}