Results 1 to 8 of 8
  1. #1
    rizowski is offline Member
    Join Date
    Nov 2010
    Posts
    37
    Rep Power
    0

    Default Redirecting Cases in a switch

    Is there a way you can tell the break to go to a certain case after?

    For ex: I don't want them to run into each other, but i do want them to receive gold with out putting hero.gold += amount; on each case.
    Java Code:
    switch(amount){
    				case 1://ITEM -> STAFF
    					item = new Staff();
    					hero.Inventory.add(item);
    					break [COLOR="YellowGreen"]default:[/COLOR];
    				case 5://ITEM -> STICKS
    					item = new Misc();
    					hero.Inventory.add(item);
    					break [COLOR="YellowGreen"]default:[/COLOR];
    		default://ONLY GOLD
    			hero.gold += amount;
    			System.out.println("You have received " + amount + " GP!!");
    			break;
    		}
    Thanks
    Last edited by rizowski; 03-17-2011 at 06:25 AM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    Maybe it's just me, but I totally can't understand what you want to achieve. A bit of clarification might be in order.

    db

  3. #3
    gauravfzd's Avatar
    gauravfzd is offline Member
    Join Date
    Oct 2010
    Location
    India
    Posts
    72
    Rep Power
    0

    Default

    Well your problem is not clear, but I think you want to execute the System.out.print statement in any case. If that is what you want you should place the print statement outside after the switch block.

    In fact looking at your switch block, I think there is no need for the switch statement because the case labels arenít doing anything special.

    by the way break statement in Switch block is basically used to separate one case bock with other, otherwise the statements below the matching case will get executed.

  4. #4
    rizowski is offline Member
    Join Date
    Nov 2010
    Posts
    37
    Rep Power
    0

    Default

    So What I am Meaning:
    Using my example above. A random number of 5 was generated.
    I should go to case 5: and be able to received the item sticks and then it should break. What i am asking is, is there a way for me to have a list of case statements and have them go to default: once it hits the break; statement?

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    No - break takes you right out of the switch.

    In the example you posted, in case you wanted the gold increment and SYstem.out.println() to happen: so just put these after the switch instead.

  6. #6
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default

    Yes.
    There's not enough detail here about your requrements.
    Does the hero get gold everytime no matter what switch statement it goes through?
    If so then simply do that code after the switch.
    If not then you'll need to give us some more details.

  7. #7
    santoshpai1 is offline Member
    Join Date
    Mar 2011
    Location
    Bangalore, India
    Posts
    5
    Rep Power
    0

    Default

    Hi ,

    I understand your requirement and the solution you are thinking is not possible. If you want to execute the some common line of code after the switch, then you can write it outside, instead writing it in one of the switch cases.

    switch(amount){
    case 1://ITEM -> STAFF
    item = new Staff();
    hero.Inventory.add(item);
    break default:;
    case 5://ITEM -> STICKS
    item = new Misc();
    hero.Inventory.add(item);
    break default:;
    default://ONLY GOLD
    System.out.println("You have received " + amount + " GP!!");
    break;
    }
    hero.gold += amount; // MOVE YOUR LINE HERE. OUTSIDE THE SWITCH.

  8. #8
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default

    Which doesn't work if cases 2, 3 and 4 shouldn't involve changing the gold...

    Which is why I said the requirements are not detailed enough.

Similar Threads

  1. Redirecting System.err on two output
    By ZioBafio in forum Advanced Java
    Replies: 2
    Last Post: 12-26-2010, 11:19 AM
  2. Help with switch cases
    By bossanova in forum New To Java
    Replies: 9
    Last Post: 11-06-2010, 02:43 AM
  3. Need help in redirecting to a url
    By umapathy_sekar in forum Advanced Java
    Replies: 1
    Last Post: 09-27-2010, 12:52 PM
  4. Replies: 3
    Last Post: 08-13-2009, 12:56 AM

Posting Permissions

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