2012-02-19 196 views
4

我想知道我怎麼可以使用變量a[i][j]的方法Scores()使用它在下面的代碼的方法MD()sumD()如何在另一種方法中使用一種方法的變量?

在我的代碼,該方法MD()sumD()不能得到的結果。

public class Test3 { 

    public void Scores() { 
    double[][] a= new double[3][5]; 
    int i,j; 

    for(i=0; i<3; i++){ 
     for(j=0; j<5; j++){ 
       a[i][j]= (double) Math.random(); 
       System.out.println("a[" + i + "][" + j + "] = " +a[i][j]); 
     } 
    } 
} 
    public void MD(){ 
    double[][] b =new double[3][5]; 
    int [] m = new int[5]; 
    int i,j; 
    //double[][] a= new double[3][5]; 

    for(j= 0; j<5; j++) 
     for(i=0 ; i<3 ; i++) 
     { 
      b[i][j]=0.0;              
      if(a[i][j]>0.0) 
       m[j]++; 
     } 
    for(j= 0; j<5; j++){ 
     for(i=0 ; i<3 ; i++) { 
      if(a[i][j] > 0.0){ 
       b[i][j]=a[i][j]*m[j]; 
       System.out.println("b[" + i + "][" + j + "] = " + b[i][j]); 
      }  
     }   
    }     
} 

public void sumD(){ 

int i,j,n; 
double[] sum= new double[3]; 
double[] k= new double[3]; 
//double[][] a= new double[3][5]; 

    for(i=0; i<3; i++){ 
     n=0; 
     sum[i]=0.0; 
     for(j=0; j<5; j++){ 
      if(a[i][j]>0.0){ 
       sum[i] += (a[i][j])*2; 
       n++; 
      }     
     } 
     k[i]=sum[i]/n; 
     System.out.println("k[" + i + "] = " + k[i]); 
} 
} 

public static void main(String[] args){ 
    Test3 print= new Test3(); 
    print.Scores(); 
    print.MD(); 
    print.sumD(); 

} 
} 

在此先感謝。

+0

把它作爲一個參數。 – 2012-02-19 18:37:49

+0

public class Test3 {private double [] [] a = new double [3] [5]; – 2012-02-19 18:44:04

回答

13

你不能。在方法中定義的變量對於該方法來說是局部的。

如果你想在方法之間共享變量,那麼你需要指定它們作爲類的成員變量。或者,您可以將它們從一個方法傳遞給另一個方法(這並不總是適用)。

+0

所以在我的代碼我應該做什麼?你能舉個例子嗎? – Jame 2012-02-19 18:40:34

2

只是移動的a聲明,使其Test3類的私有財產,是這樣的:

public class Test3 { 

    private double[][] a; 
    public void Scores() { 
    a= new double[3][5]; 
    int i,j; 
...etc... 
+0

好的。非常感謝!!!! – Jame 2012-02-19 18:49:11

1

只是做一個[i] [j]作爲類變量,其聲明的Scores()外,只下面的類名

public class Test3 { 
    double[][] a= new double[3][5]; 
    public void Scores() { 
    .... 
    } 
    ..... 
} 
+0

或者你可以稍後初始化一個它,你的選擇...... – Parth 2012-02-19 18:45:32

0

傳遞一個參數或進行類變種

0

如果你希望能夠使用2d矩陣a,你需要在你的方法之外聲明它。

public class Test3 { 
    double[][] a= new double[3][5]; 

    public void Scores() { 
     //double[][] a= new double[3][5]; 
     int i,j; 

     for(i=0; i<3; i++){ 
      for(j=0; j<5; j++){ 
      a[i][j]= (double) Math.random(); 
      System.out.println("a[" + i + "][" + j + "] = " +a[i][j]); 
      } 
     } 
    } 
    ....... 

你會看到,我已經感動的聲明的功能外(內得分(前身),並我註釋掉),它現在是類Test3的一個領域。 當你在一個函數中聲明一個變量時,它是該函數的本地變量。它不能被其他函數看到,等等。如果你將它聲明爲一個類字段,你的函數可以看到它。看看這個,因爲它可能有幫助。 Language/VariableScope.htm「> http://www.java2s.com/Tutorial/Java/0020_Language/VariableScope.htm 術語」範圍「只是指變量的生命週期以及它可以被看到的地方。 !

+0

注意:如果你這樣做並且從方法'public static void main(String args)'測試代碼,那麼你將需要創建一個類的實例通過做'Test3實例=新的Test3()'。然後用'instance.Scores()'調用該方法。 – 2012-02-19 19:03:41

5

看起來你正在使用的實例方法,而不是靜態的的。

如果你不想創建一個對象,你應該聲明所有的方法靜態的,所以像

private static void methodName(Argument args...) 

如果你希望所有這些方法都可以訪問一個變量,你應該在方法之外初始化它並限制它的範圍,聲明它是隱含的TE。

private static int[][] array = new int[3][5]; 

全局變量通常被看低(尤其是像你的一個情況下),因爲在一個大型的計劃,他們可以肆虐,所以使它的私人會防止在至少一些問題。

另外,我會說通常的:你應該儘量保持你的代碼有點整齊。使用描述性類,方法和變量名稱,並保持代碼整齊(具有適當的縮進,換行符等)並保持一致。

這裏是你的代碼應該是什麼樣子的最終(縮短)例如:

public class Test3 { 
    //Use this array in your methods 
    private static int[][] scores = new int[3][5]; 

    /* Rather than just "Scores" name it so people know what 
    * to expect 
    */ 
    private static void createScores() { 
     //Code... 
    } 
    //Other methods... 

    /* Since you're now using static methods, you don't 
    * have to initialise an object and call its methods. 
    */ 
    public static void main(String[] args){ 
     createScores(); 
     MD(); //Don't know what these do 
     sumD(); //so I'll leave them. 
    } 
} 

理想的情況下,由於您使用的一個數組,你將創建一個main方法的陣列,並把它作爲一個在每種方法中都有爭論,但是解釋它的工作原理可能是一個全新的問題,所以我將在此處予以說明。

+0

非常感謝大家!現在我懂了。 – Jame 2012-02-19 20:06:22

0
public class Test3 { 
    double[][] a=new double[3][5]; 
    public void Scores() { 

    int i,j; 

    for(i=0; i<3; i++){ 
     for(j=0; j<5; j++){ 
       a[i][j]= (double) Math.random(); 
       System.out.println("a[" + i + "][" + j + "] = " +a[i][j]); 
     } 
    } 
} 
    public void MD(){ 
    double[][] b =new double[3][5]; 
    int [] m = new int[5]; 
    int i,j; 
    //double[][] a= new double[3][5]; 

    for(j= 0; j<5; j++) 
     for(i=0 ; i<3 ; i++) 
     { 
      b[i][j]=0.0;              
      if(a[i][j]>0.0) 
       m[j]++; 
     } 
    for(j= 0; j<5; j++){ 
     for(i=0 ; i<3 ; i++) { 
      if(a[i][j] > 0.0){ 
       b[i][j]=a[i][j]*m[j]; 
       System.out.println("b[" + i + "][" + j + "] = " + b[i][j]); 
      }  
     }   
    }     
} 

public void sumD(){ 

int i,j,n; 
double[] sum= new double[3]; 
double[] k= new double[3]; 
//double[][] a= new double[3][5]; 

    for(i=0; i<3; i++){ 
     n=0; 
     sum[i]=0.0; 
     for(j=0; j<5; j++){ 
      if(a[i][j]>0.0){ 
       sum[i] += (a[i][j])*2; 
       n++; 
      }     
     } 
     k[i]=sum[i]/n; 
     System.out.println("k[" + i + "] = " + k[i]); 
    } 
} 

public static void main(String[] args){ 
    Test3 print= new Test3(); 
    print.Scores(); 
    print.MD(); 
    print.sumD(); 

} 
} 
1

你可以聲明一個二維數組指針作爲類的成員。然後在成員函數中聲明大小和值。在這種情況下,你可以訪問它形成另一個功能..

看到使用T [] []在這個代碼

public class Knapsack { 

    //private static Scanner in; 
    int T[][]; 

    int MaximumVal(int wt[],int val[], int total){ 
     int l=total; 
     int m=val.length; 
     T = new int[m+1][l+1]; 

     for (int i=0; i<=m; i++){ 
      for(int j=0; j<=l; j++){ 
       if(i==0 || j==0){ 
        T[i][j]=0; 
        continue; 
       } 
       if(j-wt[i-1] >= 0){ 
        T[i][j]=Math.max(T[i-1][j], val[i-1]+T[i-1][j-wt[i-1]]); 
       } 
       else{ 
        T[i][j]=T[i-1][j]; 
       } 
      } 
     } 
     return T[m][l]; 
    } 

    void PrintPath(int wt[], int val[]){ 
     int i=T.length-1; 
     int j=T[0].length-1; 
     while(j!=0){ 
      if(i>0){ 
       while(T[i][j]==T[i-1][j]){ 
        i--; 
       } 
      } 
      System.out.print(wt[i-1]+" "); 
      j=j-wt[i-1]; 
      i--; 
     } 
    } 


    //Main Function with test case :: 

    public static void main(String args[]){ 
     int wt[]={1,3,4,5}; 
     int val[]={1,4,5,7}; 
     Knapsack K = new Knapsack(); 

     System.out.print("Enter the total value: "); 
     //in = new Scanner(System.in); 
     //int total = in.nextInt(); 

     int result = K.MaximumVal(wt,val,7); // pass total 
     System.out.println("Total value is "+ result); 

     K.PrintPath(wt,val); 
    } 
} 
-2
public class Practise { 
    String a; 

    public String getA() { 
     return a; 
    } 
    public void setA(String a) { 
     this.a = a; 
    } 
    void meth1(){ 

     this.setA("asd"); 

    System.out.println(a); 


    } 
    void meth2(){ 

     String b="qwerty"; 
     System.out.println(getA()+" "+b); 

    } 


    public static void main(String[] args) { 

     Practise p= new Practise(); 
     p.meth1(); 
     p.meth2(); 
    } 

} 
相關問題