I've written a program where users can choose between two games to play: Pig or Snake. I tried to use the following code within my main method to create the appropriate kind of game:
if (gameType == 'p')
PigGame game = new PigGame();
else
SnakeGame game = new SnakeGame();
My compiler points to the second line and gives the error: not a statement
I have managed to fix the problem by writing an abstract class "Game" and then making PigGame and SnakeGame subclasses of it:
Game game;
if (gameType == 'p')
game = new PigGame();
else
game = new SnakeGame();
But I don't understand why the first construct didn't work. (I'm preparing to teach a high school programming course, but I'm new to Java and OOP so I can use any insights you can provide.)