当前位置:编程学习 > C#/ASP.NET >>

简单的程序错误,请大家指教

namespace ComplexEX2
{
    class Program
    {
        static void Main(string[] args)
        {
            Complex num1 = new Complex(2, 3);
            Complex num2 = new Complex(3, 4);

            Complex result = new Complex();
            result.ToString();

            Console.WriteLine(""+result .ToString());
            Console.Read();
        }
        
            struct Complex
            {
                public int real;
                public int  imaginary;
               
                }
                public static Complex operatorComplex(Complex c1, Complex c2)
                {
                    Complex result=new Complex ();
                    result.real=c1.real+c2.real ;
                    result .imaginary=c2 .imaginary +c2.imaginary ;
                    return result;
                }
        public string printComplex()
        {
            string shorw = (this.realPart + "+" + this.imagePart + "i");
            return shorw;
        }

}

--------------------编程问答-------------------- 这个似乎不怎么简单耶………… --------------------编程问答-------------------- struct Complex
{
public int real;
public int imaginary;

}
哥们,你定义的是结构体,怎么又写
Complex num1 = new Complex(2, 3);
Complex num2 = new Complex(3, 4);
应该如下
Complex num1,num2;
num1.real=2,num1.imaninnary=3;
num2.real=3,num2.imaninnary=4;
还有你的函数没参数,又在应用时有参数,建议找本书好好看看 --------------------编程问答-------------------- namespace ComplexEX2
{
 class Program
 {
    static void Main(string[] args)
     {
        Complex num1 = new Complex(2, 3);//①结构的构造函数的工作方式有一些区别,尤其是编译器总是                 提供一个无参数的默认构造函数,这是不允许代替的。 这里应该是 Complex num1=new Complex();或 Complex num1;这样也行。
        Complex num2 = new Complex(3, 4);//②同上 

        Complex result = new Complex();
        result.ToString();

        Console.WriteLine(""+result .ToString());
        Console.Read();
      }
//}后面的大括号加在这里吧?
      struct Complex
      {
        public int real;
        public int imaginary;

      }

      public static Complex operatorComplex(Complex c1, Complex c2)
       {
         Complex result=new Complex ();
         result.real=c1.real+c2.real ;
         result .imaginary=c2 .imaginary +c2.imaginary ;//这里两个都是c2的imaginary?
         return result;
        }

       public string printComplex()//这个要被用到就要写成static,因为static方法无法调用非static的方法。
       {
         string shorw = (this.realPart + "+" + this.imagePart + "i");//这里的this是当前的。而program中没有2个属性 realPart 和imagePart
         return shorw;
       }
    }//这里要添加个中括号
}
按照上面的改这样编译是没错,但是你要的功能恐怕无法实现了。
补充:.NET技术 ,  .NET Framework
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,