[Solved] How do you make a certain block of code run based on user input? [closed]


Your if statements won’t work how you expect them to to. Let’s take an example to see why:

if(board = true & board != false)

This has a number of problems:

  1. You don’t need to explicitly compare to true and false. Doing so is considered bad style. if (board) is exactly equivalent to if (board == true).
  2. board = true is assignment, not comparison. You actually have this mistake in several places. This is one of the main reasons your if statements aren’t working how you expect: if (board = true) can’t possibly be false, so the else statement can’t possibly run. Check your code carefully for other cases where you’ve confused = and == (there are several).
  3. if (board == true && board != false) is redundant and may represent a misunderstanding of how boolean values work. If (board == true), then of course board != false. Think about what it would mean if board == true && board == false; under what conditions could something like this possibly be true? Keep in mind that, by definition, boolean values are either true or false (but not both and certainly not some third value); there are only 2 possible choices.

I’d definitely encourage you to review how comparison works in Java as well as taking a look at this rather excellent article on debugging. You particularly want to focus on learning how and where to set breakpoints (i.e. points where the IDE will pause execution and allow you to examine your program’s state) and how to step through your code (i.e. run your program one line at a time so you can see the effect of each line of code) with a debugger.

1

solved How do you make a certain block of code run based on user input? [closed]