我如何在其他函数中使用变量

时间:2014-01-25 17:20:55

标签: java

我的程序需要帮助。我需要在两个()中使用变量“three”,“four”但我不知道如何。 请帮忙。

public class Main {

public static void main(String[] args) {
    one()
}

public static void one(){
    Integer three;
    Integer four;
    Integer five;
    three = 3;
    four = 4;
    two();


}
public static void two(){
    five = three + four;
    System.out.println(five);
}



}

6 个答案:

答案 0 :(得分:1)

将它们设为类变量,因此将它们置于方法之外:

static Integer three;
static Integer four;
static Integer five;

public static void one(){
    three = 3;
    four = 4;
    two();    
}

one()更改为one();


或者您可以为two()方法制作参数:

public static void one(){
    Integer three;
    Integer four;
    three = 3;
    four = 4;
    two(three, four); // add parameters here
}
public static void two(Integer three, Integer four){
    Integer five; // declare five here
    five = three + four;
    System.out.println(five);
}

答案 1 :(得分:1)

  

“我需要在两个()中使用变量”三“,”四“,但我不知道如何。”

将它们作为参数传递给方法

public static void two(Integer four, Integer three){
    int five = three + four;
    System.out.println(five);
}

像这样称呼

two(four, three);

public class Main {

    public static void main(String[] args) {
        one();
    }

    public static void one() {
        Integer three;
        Integer four;
        three = 3;
        four = 4;
        two(four, three);

    }

    public static void two(Integer four, Integer three) {
        int five = three + four;
        System.out.println(five);
    }
}

输出:7

答案 2 :(得分:1)

您可以尝试将它们声明为类中的字段:

public class Main {
    private static int three;
    private static int four;
    private static int five;
    ...
}

如果这样做,则不必在方法中再次声明它们:

public static void one(){
    three = 3;
    four = 4;
    five = two();
}

或者您可以尝试将它们作为参数传递给two()方法,并返回一个值:

public static void two(int three, int four){
    return three + four;
}

然后在one()方法中:

public static void one(){
    Integer three;
    Integer four;
    Integer five;
    three = 3;
    four = 4;
    five = two(three, four); // Assign the value returned by the 'two()' method
}

您选择的方式取决于您要做的事情。所以你必须选择一个更符合你情况的那个。

答案 3 :(得分:1)

您有两种选择。

  1. 在班级将threefour声明为static,让five成为two()的返回结果:

    public class Main {
        static Integer three;
        static Integer four;
    
        // more code
        public static void one() {
            three = 3;
            four = 4;
            Integer five = two();
            System.out.println(five);
        }
    
        public static Integer two() {
            return three + four;
        }
    }
    
  2. 传入变量(最好删除静态声明,强制您使用方法one()创建对象的新实例):

    public class Main {
        public void one() {
            Integer five = two(3, 4);
            System.out.println(five);
        }
        public Integer two(Integer three, Integer four) {
            return three + four;
        }
        public static void main(String[] args) {
            new Main().one();
        }
    }
    

答案 4 :(得分:0)

你可能想要:

public static void one(){
    int five = two(3,4);
}
public static int two(int three, int four){
    int five = three + four;
    System.out.println(five);
    return five;
}

答案 5 :(得分:-6)

class F1() {
  int a;
  int b;
  void execute() {
    a = 1;
    b = 2;
    two(this);
  };
}

void two(F1 f1) {
  int c = f1.a + f1.b;
}

或在全局范围内移动局部变量声明

static int a;
void f1() {}
void f2() {};