I was making a basic nooby java tribe game, and I needed a way to return to main menu. Can anyone tell me of any methods? This is my current code (incomplete)
I want it to return to main menu if a certain number is entered after the stats are displayed.

import java.util.Random;
import java.util.Scanner;
public class SurvivalIsland {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
	
		Scanner in = new Scanner (System.in);
		Scanner user_input = new Scanner( System.in );
		
int food;
int water;
int housing;
int tech;
int pop;
int atk;
int def;
int x;
int karma;
int farmlevel;
int choice;

food=1;
water=1;
housing=1;
tech=1;
pop=6;

atk=pop*tech;
def=pop*housing+tech;

	String tribename;
	System.out.print("Enter your tribe's name: ");
	tribename = user_input.next( );
	System.out.println("You are the leader of the "+tribename+" Tribe");
	System.out.println("Enter 1 to See your tribes current stats");
	System.out.println("Enter 2 to use action points");
	System.out.println("Enter 3 to sleep");
	
	choice = in.nextInt();
	switch(choice)

	
	{//switch
	case 1:
		{//case1
			System.out.println("Your Tribes Stats");
			System.out.println("Population ("+pop+")");
			System.out.println("Water Supply ("+water+")");
			System.out.println("Housing Level ("+housing+")");
			System.out.println("Technological Level ("+tech+")");
			System.out.println("Food Supply ("+food+")");
			
			break;
		}//case1
		
	
	

	    }//switch




	}//main method

}

Recommended Answers

All 3 Replies

One way to return to an earlier piece of code is to use a loop:

begin loop
 do menu stuff
 check user's response
 do what user wanted
 go back to beginning of loop <<< this can be automatic or done by continue statement
end loop

Thanks for the help! I got it to work!

I want to prevent it from going negative and stay at 0.

At what point do you want to stop it?
Before it goes negative: test if the amount to subtract > the amount available
If it is, don't subtract it. Or set to 0 and have a deficit amount unsupplied
Or after: test if result is < 0 and set to 0

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.