TheGuy74820
TheGuy74820

Reputation: 21

Unexpected error with my class

When writing a class it is giving me an expected token error and I can not figure out how to solve it or why it is giving it to me.

Here's the code:

public class SetUpDoors {

private int DoorAmount;
private int WinningDoorAmount; 
private int[] DoorArray= new int[DoorAmount];
private int winnerSelect = 0;

for (int i = 0; i < DoorAmount; i++) {
    if (WinningDoorAmount > 0) {
        winnerSelect = (int) Math.round( Math.random());
        DoorArray[i] = winnerSelect;
        if(winnerSelect == 1) {
            WinningDoorAmount--;
        }
    }
    else {
        DoorArray[i] = 0;
    }
    DoorAmount--;
}

void setDoorAmount(int userDoors){
    DoorAmount = userDoors;
}
void setWinningDoorAmount(int userWinningDoors) {
    WinningDoorAmount = userWinningDoors;
}

}

it is giving the error on the ; at the end of private int winnerSelect = 0; and an error for the } right below DoorAmount--; The first is expected token "{" and the second is add "}" to complete block.

Upvotes: 0

Views: 54

Answers (2)

maarkeez
maarkeez

Reputation: 98

You must declare following code inside a method.

For example:

public void newMethod(){
 for (int i = 0; i < DoorAmount; i++) {
 if (WinningDoorAmount > 0) {
    winnerSelect = (int) Math.round( Math.random());
    DoorArray[i] = winnerSelect;
    if(winnerSelect == 1) {
        WinningDoorAmount--;
    }
  }
 }
 else {
    DoorArray[i] = 0;
 }
  DoorAmount--;
}

Upvotes: 2

Crammeur
Crammeur

Reputation: 688

try this

public class SetUpDoors {

    private int DoorAmount;
    private int WinningDoorAmount; 
    private int[] DoorArray= new int[DoorAmount];
    private int winnerSelect = 0;

    {
        for (int i = 0; i < DoorAmount; i++) {
        if (WinningDoorAmount > 0) {
            winnerSelect = (int) Math.round( Math.random());
            DoorArray[i] = winnerSelect;
            if(winnerSelect == 1) {
                WinningDoorAmount--;
            }
        }
        else {
            DoorArray[i] = 0;
        }
        DoorAmount--;
        }
    }

    void setDoorAmount(int userDoors){
        DoorAmount = userDoors;
    }
    void setWinningDoorAmount(int userWinningDoors) {
        WinningDoorAmount = userWinningDoors;
    }
}

Upvotes: 1

Related Questions