2017-06-17 39 views
-4

這個項目對我來說是非常有必要的。所以請有人幫我解決它..自20天以來我一直試圖解決下面提到的問題....這是我的代碼有兩種方法,我是否將變量()中的變量調用到另一個方法中?我需要將變量()中的變量用於其他函數。如何將參數化變量從其他方法調用到另一個同一類中?

下面是變量法:

public void variables(String mm,String Maths,String computer,String english,String hindi,String arts,String physics,String chemistry,String biology,int mmi,int mmf,int Math, 
      int com,int hin,int eng,int art,int chem,int bio,int phy,int sum,float fin){ 
       mm = Marks.getText(); 
      mmi = Integer.parseInt(mm); 
      mmf = mmi*8; 

      Maths = jTextField2.getText().replaceAll("\\s",""); 
      computer= jTextField6.getText().replaceAll("\\s",""); 
      english = jTextField8.getText().replaceAll("\\s",""); 
      hindi = jTextField7.getText().replaceAll("\\s",""); 
      arts = jTextField9.getText().replaceAll("\\s",""); 
      physics = jTextField3.getText().replaceAll("\\s",""); 
      chemistry = jTextField4.getText().replaceAll("\\s",""); 
      biology = jTextField5.getText().replaceAll("\\s",""); 
      Math = Integer.parseInt(Maths); 
      com = Integer.parseInt(computer); 
     hin = Integer.parseInt(hindi); 
     eng = Integer.parseInt(english); 
     art = Integer.parseInt(arts); 
     chem = Integer.parseInt(chemistry); 
     phy = Integer.parseInt(physics); 
     bio = Integer.parseInt(biology); 

     sum = Math+com+hin+eng+art+chem+phy+bio;  
     fin = (sum*100)/mmf; 


      } 

這是另一種方法:

//I want to call the variables of the Variables() method to this one..but how?? 
    public void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { 


     float flin = Third.variables(fin); 
      JOptionPane.showMessageDialog(null,"Your Percentage for this Cycle Test is: "+flin+"%"); 
      this.dispose(); 
      Fourth IV = new Fourth(); 
      IV.setVisible(true); 



     } 
+1

很難說出你實際要求的是什麼。也許你想要一個數組?這是一個志願者幫助網站;如果你想要「緊急」的幫助,你可以按我平常的速度僱用我,最少2小時。 – chrylis

+0

@chrylis我認爲你也應該告訴他你平常的速度,因爲他是堆棧溢出的新手。 –

+1

@HimanshuMittal首先你需要改善你的問題代碼塊,以便它可以很容易閱讀 –

回答

0

使用全局變量,正如我所說的int評論。

例如: -

class Print 
{ 
static int a;// i will use this variable in different functions 

void set(int x) 
{ 
a=x; // a is used here 
} 

void display() 
{ 
System.out.print(a);// a is printed(used) here 

} 

public static void main(String args[]) 
{ 
Print obj=new Print(); 
obj.set(5); 
obj.display(); 

} 


} 

同樣可以聲明儘可能多的全局變量,並利用它們在不同的功能,你want.I希望它可以幫助你,祝你好運(對不起,嘲笑)。

+0

ty太多..但你可以用我的代碼給出例子..請問 –

0

好Himanshu在這裏我試圖給你使用你的代碼的例子。

class Example 
{ 
static float fin=0.0f;// global variable declared and initialised with default value 

public void variables(String mm,String Maths,String computer,String english,String hindi,String arts,String physics,String chemistry,String biology,int mmi,int mmf,int Math,int com,int hin,int eng,int art,int chem,int bio,int phy,int sum,float fin) 
{ 
       mm = Marks.getText(); 
      mmi = Integer.parseInt(mm); 
      mmf = mmi*8; 

      Maths = jTextField2.getText().replaceAll("\\s",""); 
      computer= jTextField6.getText().replaceAll("\\s",""); 
      english = jTextField8.getText().replaceAll("\\s",""); 
      hindi = jTextField7.getText().replaceAll("\\s",""); 
      arts = jTextField9.getText().replaceAll("\\s",""); 
      physics = jTextField3.getText().replaceAll("\\s",""); 
      chemistry = jTextField4.getText().replaceAll("\\s",""); 
      biology = jTextField5.getText().replaceAll("\\s",""); 
      Math = Integer.parseInt(Maths); 
      com = Integer.parseInt(computer); 
     hin = Integer.parseInt(hindi); 
     eng = Integer.parseInt(english); 
     art = Integer.parseInt(arts); 
     chem = Integer.parseInt(chemistry); 
     phy = Integer.parseInt(physics); 
     bio = Integer.parseInt(biology); 

     sum = Math+com+hin+eng+art+chem+phy+bio;  
     fin = (sum*100)/mmf; 


     } 

// Use fin here because it is declared globally(after class before function) 
public void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { 


     float flin = fin; // Used fin directly as it is global 
      JOptionPane.showMessageDialog(null,"Your Percentage for this Cycle Test is: "+flin+"%"); 
      this.dispose(); 
      Fourth IV = new Fourth(); 
      IV.setVisible(true); 



     } 

} 
+0

謝謝你..但它會工作嗎? –

+0

我的意思是第二種方法中翅片的價值..將它更新/ –

+0

和m使用其他類中的方法 –

相關問題