Reputation: 21
In Java, are there any alternatives to using a goto statement without creating loops and breaks?
After the user has said help
(for instance), I want them to be able to type something again. Is this possible?
Here's my code:
package Main;
import java.util.Scanner;
import java.util.Calendar;
import java.text.SimpleDateFormat;
public class Start {
public static <DateFormat> void main( String[] args ) {
Scanner input = new Scanner( System.in );
Calendar.getInstance().getTime();
SimpleDateFormat dateFormat = new SimpleDateFormat( "HH:mm:ss" );
Calendar cal = Calendar.getInstance();
// creating scanner etc;
System.out.println( "Welcome To B.V.D Please Type Your Name Here: " );
String name = input.nextLine();
// getting name;
System.out.println( "For A List Of Commands " + name + " Type help" );
String main = input.nextLine();
//User input on what the user wants to do
if ( main.equals( "help" ) ) {
System.out.println( "You Can Use Commands Like These (Remember Type It Exactly As You See it) " );
System.out.println( "name, time, stopwatch, help, calculator, store, 15's" );
}
// help
if ( main.equals( "time" ) ) {
System.out.println( dateFormat.format( cal.getTime() ) + " GMT" );
}
input.close();
}
}
Upvotes: 2
Views: 238
Reputation: 167
I don't think so. I always use a while loop to do this.
And why that declaration of main function? I don't understand the DateFormat thing. It is the first time I see that. The main function doesn't return anything.
Upvotes: 0
Reputation: 3914
Just put everything in a loop and use a boolean for exiting that loop. If you are at the end of the loop, you decide if you want to ask the user again to type in stuff, and if yes, you set the variable to true, otherwise to false:
boolean running = true;
while (running) {
// Do stuff
if (/* I don't want to continue doing stuff */) {
running = false;
}
}
Upvotes: 1