Reputation: 2033
I keep getting an error, if
without else
.
I tried else if
as well
for (;;) {
System.out.println("---> Your choice: ");
choice = input.nextInt();
if (choice==1)
playGame();
if (choice==2)
loadGame();
if (choice==3)
options();
if (choice==4)
credits();
if (choice==5)
System.out.println("End of Game\n Thank you for playing with us!");
break;
else
System.out.println("Not a valid choice!\n Please try again...\n");
}
Also if you have a better idea on how to present this code please do not hesitate :)
Upvotes: 24
Views: 334043
Reputation: 338775
Use Arrow labels in Java 14+.
switch ( choice )
{
case 1 -> game.playGame ( );
case 2 -> game.loadGame ( );
case 3 -> game.options ( );
case 4 -> game.credits ( );
case 5 ->
{
System.out.println ( "End of Game\n Thank you for playing with us!" );
go = false; // Halt the game.
}
default -> System.out.println ( "Not a valid choice!\n Please try again...\n" );
As others noted, an if
test without curly braces ({…}
) executes only the code immediately following, when meeting its predicate.
if ( true )
System.out.println( "Prints only when predicate is met." ) ;
System.out.println( "Prints *always*. Has nothing to do with the `if` test." ) ;
if ( false )
System.out.println( "Prints only when predicate is met." ) ;
System.out.println( "Prints *always*. Has nothing to do with the `if` test." ) ;
See this code run at Ideone.com.
Prints only when predicate is met.
Prints *always*. Has nothing to do with the `if` test.
Prints *always*. Has nothing to do with the `if` test.
Tip: As a beginner in Java, always include the curly braces with your if
.
Modern Java (14+) offers alternate syntax for a switch
statement.
This new Arrow labels feature arrived with Switch Expressions described in JEP 361. This feature allows "case L ->" labels instead of "case L :" labels. If a label is matched, then only the expression or statement to the right of the arrow is executed — no fall-through. Instead of an else
, use default
as a catch-all case.
This alternate syntax more clearly expresses what you are thinking. For multiple lines, use curly braces, as seen in case 5
.
switch ( choice )
{
case 1 -> game.playGame ( );
case 2 -> game.loadGame ( );
case 3 -> game.options ( );
case 4 -> game.credits ( );
case 5 ->
{
System.out.println ( "End of Game\n Thank you for playing with us!" );
go = false; // Halt the game.
}
default -> System.out.println ( "Not a valid choice!\n Please try again...\n" );
Full example:
Scanner scanner = new Scanner ( System.in );
boolean go = true;
while ( go )
{
Game game = new Game ( );
System.out.println ( "---> Your choice: " );
int choice = scanner.nextInt ( );
switch ( choice )
{
case 1 -> game.playGame ( );
case 2 -> game.loadGame ( );
case 3 -> game.options ( );
case 4 -> game.credits ( );
case 5 ->
{
System.out.println ( "End of Game\n Thank you for playing with us!" );
go = false; // Halt the game.
}
default -> System.out.println ( "Not a valid choice!\n Please try again...\n" );
}
}
Here is a dummy Game
implementation so you can try that code.
class Game
{
public void playGame ( ) { System.out.println ( "Playing game." ); }
public void loadGame ( ) { System.out.println ( "Loading." ); }
public void options ( ) { System.out.println ( "Options." ); }
public void credits ( ) { System.out.println ( "Credits." ); }
}
Upvotes: 0
Reputation: 2823
Where some went there to provide alternative implementation of code the reason that the op code does not work is a missing {}
pair;
if (choice==5) {
System.out.println("End of Game\n Thank you for playing with us!");
break;
} else
Upvotes: 0
Reputation: 307
The "break" command does not work within an "if" statement.
If you remove the "break" command from your code and then test the code, you should find that the code works exactly the same without a "break" command as with one.
"Break" is designed for use inside loops (for, while, do-while, enhanced for and switch).
Upvotes: 29
Reputation: 76908
Because your else
isn't attached to anything. The if
without braces only encompasses the single statement that immediately follows it.
if (choice==5)
{
System.out.println("End of Game\n Thank you for playing with us!");
break;
}
else
{
System.out.println("Not a valid choice!\n Please try again...\n");
}
Not using braces is generally viewed as a bad practice because it can lead to the exact problems you encountered.
In addition, using a switch
here would make more sense.
int choice;
boolean keepGoing = true;
while(keepGoing)
{
System.out.println("---> Your choice: ");
choice = input.nextInt();
switch(choice)
{
case 1:
playGame();
break;
case 2:
loadGame();
break;
// your other cases
// ...
case 5:
System.out.println("End of Game\n Thank you for playing with us!");
keepGoing = false;
break;
default:
System.out.println("Not a valid choice!\n Please try again...\n");
}
}
Note that instead of an infinite for
loop I used a while(boolean)
, making it easy to exit the loop. Another approach would be using break with labels.
Upvotes: 20
Reputation: 9648
The issue is that you are trying to have multiple statements in an if
without using {}
.
What you currently have is interpreted like:
if( choice==5 )
{
System.out.println( ... );
}
break;
else
{
//...
}
You really want:
if( choice==5 )
{
System.out.println( ... );
break;
}
else
{
//...
}
Also, as Farce has stated, it would be better to use else if
for all the conditions instead of if
because if choice==1
, it will still go through and check if choice==5
, which would fail, and it will still go into your else block.
if( choice==1 )
//...
else if( choice==2 )
//...
else if( choice==3 )
//...
else if( choice==4 )
//...
else if( choice==5 )
{
//...
}
else
//...
A more elegant solution would be using a switch
statement. However, break
only breaks from the most inner "block" unless you use labels. So you want to label your loop and break from that if the case is 5:
LOOP:
for(;;)
{
System.out.println("---> Your choice: ");
choice = input.nextInt();
switch( choice )
{
case 1:
playGame();
break;
case 2:
loadGame();
break;
case 2:
options();
break;
case 4:
credits();
break;
case 5:
System.out.println("End of Game\n Thank you for playing with us!");
break LOOP;
default:
System.out.println( ... );
}
}
Instead of labeling the loop, you could also use a flag to tell the loop to stop.
bool finished = false;
while( !finished )
{
switch( choice )
{
// ...
case 5:
System.out.println( ... )
finished = true;
break;
// ...
}
}
Upvotes: 9