Results 1 to 2 of 2
  1. #1
    CDog1324 is offline Member
    Join Date
    Jun 2012
    Posts
    1
    Rep Power
    0

    Default If Statement question

    So first of all, here is my code:
    Java Code:
    import java.util.Scanner;
    
    public class Main {
    	public static void main(String args[]){
    		Scanner s = new Scanner(System.in);
    		
    		System.out.println("Welcome New Player!");
    		System.out.println("I will by the guide on your Adventure through the land of Marner.");
    		System.out.println("As your first choose, do you want to be a Mage, a Warrior, or a Archer?\n");
    		System.out.println("Options:\n1: Mage\n2: Warrior\n2: Archer\n Now type either 1, 2, or 3.\n");
    		
    		System.out.print("Choice: ");
    		int a = s.nextInt();
    		
    		if(a == 1)
    			System.out.println("You have chosen to be a Mage!\nYou will be mentored by the Arcane God Ryze!\nGood Luck on your Travels!");
    		else if(a == 2)
    			System.out.println("You have chosen to be a Warrior!\nYou will be mentored by the Rage God Tryndamere!\n Good luck on your Travels!");
    		else if(a == 3)
    			System.out.println("You have chosen to be a Archer!\nYou will be mentored by the Shadow God Shaco!\n Good luck on your Travels!");
    		else
    			System.out.println("You have chosen an invalid choice, and will now be prompted to chose again.\n Remember, choose either 1, 2, or 3.");
    		}
    		}
    So basically, i want to have them choose their class, and when chosen, it displays what they've chosen, etc. But what I want to know, is if they choose press something other than 1, 2, or 3, how to make it loop back to the beginning, and have them choose again. I know it's a little bit of a noob question, but i can't seem to figure it out :P

    Thanks!!
    Last edited by JosAH; 06-03-2012 at 09:30 AM. Reason: added [code] ... [/code] tags

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: If Statement question

    The answer is a loop: keep on looping while no correct choice has been made; the following is a bit of a spoiler: it takes advantage that a boolean assignment is a boolean expression and it can be used in your if statements:

    Java Code:
    for (boolean correct= false; !correct; ) {
       System.out.print("Choice: ");         
       int a = s.nextInt();
       if(correct= (a == 1))
          System.out.println("You have chosen to be a Mage!\nYou will be mentored by the Arcane God Ryze!\nGood Luck on your Travels!");
       else if(correct= (a == 2))   
          System.out.println("You have chosen to be a Warrior!\nYou will be mentored by the Rage God Tryndamere!\n Good luck on your Travels!");
       else if(correct= (a == 3))      
          System.out.println("You have chosen to be a Archer!\nYou will be mentored by the Shadow God Shaco!\n Good luck on your Travels!");    
       else       
          System.out.println("You have chosen an invalid choice, and will now be prompted to chose again.\n Remember, choose either 1, 2, or 3.");         
    }
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 2
    Last Post: 03-26-2012, 05:27 PM
  2. Newbie Question-Exponents & Missing Return Statement
    By dgoff267 in forum New To Java
    Replies: 6
    Last Post: 02-21-2012, 08:27 PM
  3. Question about an if statement.
    By VisionIncision in forum New To Java
    Replies: 2
    Last Post: 01-08-2011, 01:28 PM
  4. Beginner - question of 'if' statement
    By hayden06f4i in forum New To Java
    Replies: 6
    Last Post: 11-08-2010, 03:45 AM
  5. JDBC statement question
    By nick2price in forum Advanced Java
    Replies: 21
    Last Post: 09-29-2008, 03:09 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •