Reputation: 39
OK, so I'm sure this question has been beaten to death, but I still can't find the answer I need for my project.
I am working on building a casino game suite in java (I'm 110% sure that there are better, and easier languages for this, but I am learning java right now and I'd like to code in java for the practice.) My issue is that I cannot figure out how to structure my code. I am used to using goto statements (I started learning coding in small basic). For example:
import java.util.*;
public class CasinoGames
{
public static void main(String[] args)
{
Scanner keys = new Scanner(System.in);
sopln("Hello, and welcome to Casino Games!");
sopln("Would you like to login, register, or play as a guest?");
char token = keys.nextLine().toLowerCase().charAt(0);
randomlabelhere:
if (token == "l")
User.login();
else if (token == "r")
User.register();
else if (token == "g")
User.guestLogin();
else
sopln("Invalid Choice, please try again!");
goto somerandomlabel
I know this won't compile, so please don't mention that. I know that I can use a do-while loop for this, but if I wanted the option to do a goto, what alternatives do I have?
Upvotes: 0
Views: 6345
Reputation:
import java.util.Scanner;
public class Casino {
public static void main(String[] args) {
try (Scanner input = new Scanner(System.in)) {
sopln("Hello, and welcome to Casino Games!");
sopln("Would you like to login, register, or play as a guest?");
while (true) {
char token = input.next().toLowerCase().charAt(0);
if (token == 'l') {
User.login();
break;
} else if (token == 'r') {
User.register();
break;
} else if (token == 'g') {
User.guestLogin();
break;
} else {
sopln("Invalid Choice, please try again!");
}
}
}
}
public static void sopln(String str) {
System.out.println(str);
}
private static class User {
static void login() {}
static void register() {}
static void guestLogin() {}
}
}
Scanner implements AutoCloseable in Java 7+, so I managed it with the try-with-resources feature.
Note: User should be presented with a menu so they know what l, r, and g stand for. I know it seems obvious, but you'd be surprised :-P
I'd also add some sort of escape case for exiting the program.
I'm trying to think of the last time I used a goto statement, and the only thing that comes to mind is MIPS assembly jump operations.
Upvotes: 0
Reputation: 793
If you wanted to, you could take everything that would be under the goto
and make it it's own method. Say you wanted goto startInteraction
, then you could do this:
void startInteraction(){
//everything below your "randomlabelhere:" would go in this method
}
and then call startInteraction()
any time you wanted to trigger that.
A difference is that a goto
wouldn't return to where it was called from, while this method would. That feature could be simulated if you used a return;
in the line following where you call startInteraction()
so that the code doesn't continue following the method call.
Upvotes: 2
Reputation: 347194
for-loop
while-loop
do-while-loop
goto
(IMHO)Also, token == "l"
is not how String
comparison works in Java, you want to something more like "l".equals(token)
Besides, token
is a type of char
so it should be more like token == '1'
, but you could get away with using a String
and using token.trim().startsWith("1")
, but you'd need to test for a null
result
Upvotes: 7
Reputation: 14025
Here's one way you could do it:
import java.util.*;
public class CasinoGames
{
public static void main(String[] args)
{
Scanner keys = new Scanner(System.in);
sopln("Hello, and welcome to Casino Games!");
sopln("Would you like to login, register, or play as a guest?");
while (true) {
char token = keys.nextLine().toLowerCase().charAt(0);
if (token == 'l') {
User.login();
break;
}
else if (token == 'r') {
User.register();
break;
}
else if (token == 'g') {
User.guestLogin();
break;
}
else {
sopln("Invalid Choice, please try again!");
}
}
}
}
Upvotes: 1
Reputation: 201439
The closest thing Java has to a goto
are labels for break
and continue
. Goto has been considered harmful for longer than Java has been a language, and consequently Java doesn't have a goto implementation (and goto
is a reserved word so you cannot add one). Finally, since token
is a char
you should compare with char
literals like
while(true) {
if (token == 'l')
User.login();
else if (token == 'r')
User.register();
else if (token == 'g')
User.guestLogin();
else {
sopln("Invalid Choice, please try again!");
continue;
}
break;
}
Upvotes: 2