2013-10-26 47 views
-4

我嘗試爲android設計一個應用程序。在java中更改變量值

我想,輕敲按鈕1,x設置爲1,併爲BUTTON2 y設置爲1,等等。當

然後在if語句中使用這些變量。

int z; 
int y; 
int w; 
final Button m = (Button) findViewById(R.id.button1); 
final Button m1 = (Button) findViewById(R.id.button4); 
final Button m2 = (Button) findViewById(R.id.button5); 
m.setOnTouchListener(new OnTouchListener() { 
    @Override 
    public boolean onTouch(View arg0, MotionEvent arg1) { 
     // some code here 
     z == 1; 
     return false; 
    } 
}); 
m1.setOnTouchListener(new OnTouchListener() { 
    @Override 
    public boolean onTouch(View arg0, MotionEvent arg1) { 
     // some code here 
     z == 1; 
     return false; 
    } 
}); 
m2.setOnTouchListener(new OnTouchListener() { 
    @Override 
    public boolean onTouch(View arg0, MotionEvent arg1) { 
     // some code here 
     z == 1; 
     return false; 
    } 
}); 
if (x == 1 & y == 1 & z == 1) { 
    finish(); 
    //some code here 
} 

但是「if」內部塊永遠不會到達;我該如何解決這個問題?

謝謝。

+0

你應該學會如何正確地格式化代碼。關於你的問題:Hexafraction是正確的。你必須使用一個等號來爲你的變量賦值。 – Ahmad

+0

您應該使用'&&'作爲邏輯,並且像這樣'if(x == 1 && y == 1 && z == 1)'。一個'&'是一個按位運算符,如果一切都是奇數,但不會按照您的想法操作,則該運算符將起作用。 – Simon

+0

==只是錯的類型 我的問題:「如果」沒有工作(我試過&&但還沒有工作) – meysam

回答

1
z==1; 

是錯誤的。 ==用於比較值,不用於設置。使用方法:

z=1; 

改爲。無論如何,在跳入Android之前,我會學習更多的Java基礎知識。


這可能會發生,所以我最好現在而不是以後告訴你。不應使用==而是使用string1.equals(string2)來比較字符串。

+0

==是錯誤的類型我知道它的比較我只是趕緊鍵入記事本並粘貼在這裏 我的問題是「如果」不起作用! – meysam

+0

@meysam如果你指定錯誤,會導致ifs無法正常工作。 – hexafraction