2012-07-12 46 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()

相關問題