2012-07-12 44 views
0

这是我的代码:对象引用是非静态字段的要求。 。 。 。 ,错误

的目的是为了在2阵列添加两个大的数字,首先输入两个数字,然后swaping两者并添加它们,控制台 - 基于,

我是一个新手C#所以请解释记住我的代码

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
namespace Sum_Two_BIG_Numbes 
{ 
    public class matrix 
    { 

     public int[] c; 

     public void input(int[] a) 
     { 
      for (int i = 0; i < a.Length; i++) 
      { 

       a[i] = Convert.ToInt32(Console.ReadLine()); 

      } 
     } 
     public void Jamk(int[] a, int[] b, int[] c) 
     { 

      for (int i = 0; i < a.Length; i++) 
      { 
       int temp = a[i] + b[i]; 
       if ((temp < 10) && (c[i] != 1)) 
       { 
        c[i] = c[i] + temp; 
       } 
       else 
       { 
        c[i] = c[i] + temp % 10; 
        c[i + 1] = c[i + 1] + temp/10; 

       } 
      } 


     } 
     public void swap(int[] a) 
     { 

      for (int i = 0; i < a.Length; i++) 
      { 
       a[i] = a[a.Length - i]; 
      } 

     } 
    } 
    class Program 
    { 
     public void Main(string[] args) 
     { 
      int[] a = new matrix(); 
      //int[] a = new int[30]; 
      int[] b = new int[30]; 
      int[] c = new int[30]; 
      Console.WriteLine("Enter First Number : "); 
      matrix.input(a); 


      Console.ReadLine(); 
     } 
    } 
} 

我得到这个错误“的对象Refrence需要选用是对于非静态字段。。”

+0

使用读了哪条线中的代码显示的错误? – 2012-07-12 20:17:36

+0

最简单的方法来改变这个并获得访问权限将是使用关键字“static”将公共静态方法更改为公共静态 – MethodMan 2012-07-12 20:20:18

+0

可能的[非静态字段,方法或属性'WindowsApplication1.Form1。 setTextboxText(INT)](http://stackoverflow.com/questions/498400/an-object-reference-is-required-for-the-nonstatic-field-method-or-property-wi) – Spontifixus 2014-03-19 17:48:11

回答

2
matrix.input(a); 
的几个知识

matrix在哪里声明(不是)。

int[] a = new matrix(); 

此外,您不能的matrix实例分配给int[]。不存在从一个到另一个的隐式转换。虽然我不能说这是一个伟大的设计,你想要的是这样的:

matrix a = new matrix(); 
a.c = new int[SomeSize]; 
// more code 
a.input(b); // or something similar... 
0

您需要创建Main方法内部类矩阵的一个新实例是这样的: 矩阵实例=新的矩阵(); 然后你可以调用矩阵类的方法。阅读静态和实例方法/属性/字段之间的差异。

0

你的代码并没有真正意义上的,但这里是我想你的意思

public void Main(string[] args) 
     { 
      matrix m = new matrix(); 
      int[] a = new int[30]; 
      int[] b = new int[30]; 
      int[] c = new int[30]; 
      Console.WriteLine("Enter First Number : "); 
      m.input(a); 


      Console.ReadLine(); 
     } 

现在,这将让你闯过首轮编译错误的,但它仍然没有任何意义。关于你的输入/输出。

我建议你上Console.ReadLine()

相关问题