0

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.

asked Nov 5, 2017 at 22:37
2
  • The for loop needs to be inside a method... Commented Nov 5, 2017 at 22:40
  • 1
    You can't just dump code in a class, you need a method. Commented Nov 5, 2017 at 22:40

2 Answers 2

2

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--;
}
answered Nov 5, 2017 at 22:46
Sign up to request clarification or add additional context in comments.

Comments

1

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;
 }
}
answered Nov 5, 2017 at 22:46

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.