Reputation: 3
I am new to java and the coding is probably sloppy so please don't be harsh! Anyways, I am making a method that will set increase in turn by 1 each time I hit the end turn button.
public int turns (int turn){
int turns = 0;
if (turn == 0){
btnYes.setEnabled(false);
btnNo.setEnabled(false);
btnRolldie.setEnabled(true);
btnPurchase.setEnabled(true);
btnMove.setEnabled(true);
turn++;
}
if (turn == 1){
btnYes.setEnabled(false);
btnNo.setEnabled(false);
btnRolldie.setEnabled(true);
btnPurchase.setEnabled(true);
btnMove.setEnabled(true);
turn++;
}
if (turn == 2){
btnYes.setEnabled(false);
btnNo.setEnabled(false);
btnRolldie.setEnabled(true);
btnPurchase.setEnabled(true);
btnMove.setEnabled(true);
turn++;
}
if (turn == 3){
btnYes.setEnabled(false);
btnNo.setEnabled(false);
btnRolldie.setEnabled(true);
btnPurchase.setEnabled(true);
btnMove.setEnabled(true);
turn = 0;
}
return(turns);
}
private void btnEndTurnActionPerformed(java.awt.event.ActionEvent evt) {
int turns = 0;
lblturn.setText("" +turns(turns));
When I run the program, the label constantly repeats 0 whenever I hit the button. I was thinking that int turns; would work, but the variable isn't initialized. I do not know if there is an error in my method, or if the initialization in the button is overriding the method.
As I said, i'm new to java so this might be the entirely wrong approach, and if it is, please give me some recommendations on how to improve this kind of structure. Thanks!
Upvotes: 0
Views: 48
Reputation: 8969
int turns = 0;
on the second line is a local variable so the value get reset every time the method is called. You need to make it as a field of a class so that it can be shared between method. And also change turn++
to turns++
.
Upvotes: 2