如何将参数化变量从其他方法调用到同一个类的另一个?

时间:2017-06-17 07:43:42

标签: java variables methods

这个项目对我来说是非常必要的..所以请有人帮助我做它..我一直试图解决下面提到的问题20天....这是我的代码有两种方法,如何将变量()中的变量调用到另一个方法中?我需要将variables()中的变量用于另一个函数。

以下是变量方法:

for(int i=0; i < myBooks.length; i++){
   if(myBooks[i].getISBN().equals("Specific ISBN that you want to look for")){
      // enter code here
   }

}

这是另一种方法:

 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;


            }

2 个答案:

答案 0 :(得分:0)

正如我在评论中所说的那样使用全局变量。

例如: -

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();

}


}

同样地,您可以声明尽可能多的全局变量,并根据需要在不同的函数中使用它们。我希望它可以帮助您并祝您好运(抱歉嘲笑)。

答案 1 :(得分: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);



         }

}