2014-01-25 33 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; 
}