Results 1 to 2 of 2
Thread: If Statement question
- 06-03-2012, 08:05 AM #1
Member
- Join Date
- Jun 2012
- Posts
- 1
- Rep Power
- 0
If Statement question
So first of all, here is my code:
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 :PJava 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."); } }
Thanks!!Last edited by JosAH; 06-03-2012 at 08:30 AM. Reason: added [code] ... [/code] tags
- 06-03-2012, 09:13 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,381
- Blog Entries
- 7
- Rep Power
- 17
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:
kind regards,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."); }
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
the switch statement and unreachable statement error
By name in forum New To JavaReplies: 2Last Post: 03-26-2012, 04:27 PM -
Newbie Question-Exponents & Missing Return Statement
By dgoff267 in forum New To JavaReplies: 6Last Post: 02-21-2012, 07:27 PM -
Question about an if statement.
By VisionIncision in forum New To JavaReplies: 2Last Post: 01-08-2011, 12:28 PM -
Beginner - question of 'if' statement
By hayden06f4i in forum New To JavaReplies: 6Last Post: 11-08-2010, 02:45 AM -
JDBC statement question
By nick2price in forum Advanced JavaReplies: 21Last Post: 09-29-2008, 02:09 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks