Results 1 to 8 of 8
Thread: Redirecting Cases in a switch
- 03-17-2011, 04:56 AM #1
Member
- Join Date
- Nov 2010
- Posts
- 37
- Rep Power
- 0
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.
ThanksJava 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; }Last edited by rizowski; 03-17-2011 at 06:25 AM.
- 03-17-2011, 06:00 AM #2
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
- 03-17-2011, 06:12 AM #3
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.
- 03-17-2011, 06:21 AM #4
Member
- Join Date
- Nov 2010
- Posts
- 37
- Rep Power
- 0
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?
- 03-17-2011, 07:13 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,548
- Rep Power
- 11
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.
- 03-17-2011, 09:04 AM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 03-17-2011, 09:37 AM #7
Member
- Join Date
- Mar 2011
- Location
- Bangalore, India
- Posts
- 5
- Rep Power
- 0
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.
- 03-17-2011, 10:09 AM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Similar Threads
-
Redirecting System.err on two output
By ZioBafio in forum Advanced JavaReplies: 2Last Post: 12-26-2010, 11:19 AM -
Help with switch cases
By bossanova in forum New To JavaReplies: 9Last Post: 11-06-2010, 02:43 AM -
Need help in redirecting to a url
By umapathy_sekar in forum Advanced JavaReplies: 1Last Post: 09-27-2010, 12:52 PM -
I don't understand: a=1;b=2;a=b;a=4 results in b=4 (in complex cases w/ iterator)
By gymshoe in forum New To JavaReplies: 3Last Post: 08-13-2009, 12:56 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks