2014-01-25 32 views
-1

我需要我的程序帮助。我需要在两个()中使用变量“三”,“四”,但我不知道如何。 请帮忙。如何在其他功能中使用变量

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



} 
+0

你定义他们的路,他们是当地只有你声明它们在方法声明。他们作为类变量,而不是任何方法,那么你可以在方法中实例化或设置它们的值。 – csmckelvey

+0

将这些变量声明为类变量。然后创建你的班级的对象并给他们打电话。先了解基本的课程,对象,功能。 –

+0

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html – Radiodef

回答

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); 
} 
+0

谢谢,但后来我得到的错误是: – bigfatlemon

+0

无法对静态字段进行静态引用3 无法创建静态引用非静态字段四 – bigfatlemon

+0

@bigfatlemon看看我的更新代码。我将它们改为静态变量。 –

1

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

将它们传递到方法的参数

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

OUTPUT:7

+0

为什么倒票?有用 –

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 
} 

您选择的方式取决于什么你正在尝试做的。所以你必须选择一个更适合你的案例。

1

你有两种选择。

  1. 在类级别声明threefourstatic,让fivetwo()返回的结果:

    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(); 
        } 
    } 
    
-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() {}; 
0

您probaly想:

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; 
}