Results 1 to 17 of 17
Thread: java switch case
- 08-08-2011, 07:45 AM #1
Member
- Join Date
- May 2011
- Posts
- 39
- Rep Power
- 0
- 08-08-2011, 07:48 AM #2
In some languages you can but in Java you cannot. What you do instead is omit the break statement.
Java Code:switch number { case 1: case 2: case 3: // do stuff break; case 4: case 5: // etc }
- 08-08-2011, 07:50 AM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
No, Java (nor C++ nor C) alloq that notation; you have to list each possibility separate, e.g.
This is undoable for large ranges and you have to change your statement to a sequence of if-else-if-else ... statements.Java Code:switch (x) { case 1: case 2: case 3: ...
kind regards,
Jos
edit: Monday morning and too slow again ...When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 08-08-2011, 07:52 AM #4
Member
- Join Date
- May 2011
- Posts
- 39
- Rep Power
- 0
but I have all these different values:
0 - 49.5
50 - 59.5
and so on...
so I will use the if else statement
- 08-08-2011, 07:54 AM #5
Yep, if you have too many cases then an if statement is a better choice.
- 08-08-2011, 07:54 AM #6
Member
- Join Date
- May 2011
- Posts
- 39
- Rep Power
- 0
thanks a lot very quick !
- 08-08-2011, 08:18 AM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 08-09-2011, 12:25 AM #8
Since the case only works with int variables use if statements, it will work on many types. Also if you have a MUCH larger range the case statements will be many.
number is type double in the example below.
Java Code:if( number >= 1.0 || number <= 40.0 ) System.out.println( "Very low mark" ); if( number >= 41.0 || number <= 70.0 ) System.out.println( "Low mark" ); if( number >= 71.0 || number <= 100.0 ) System.out.println( "Better mark" );Last edited by stchman; 08-09-2011 at 12:29 AM.
If you aren't programming in Java, well that's just too bad.
I'd rather be using Ubuntu.
- 08-09-2011, 12:32 AM #9
Looks like I actually helped someone in this thread. Are you happy now?
- 08-09-2011, 12:40 AM #10
Yes, and you even managed to curb your urge to berate them as well. There might be hope yet.
If you aren't programming in Java, well that's just too bad.
I'd rather be using Ubuntu.
- 08-09-2011, 06:37 AM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
- 08-09-2011, 06:47 AM #12
I think you could probably do
case > 0; case < 4:
just a guess, but it might work
- 08-09-2011, 06:51 AM #13
Don't make guesses and provide incorrect advice. Try it yourself first before posting.
- 08-09-2011, 06:53 AM #14
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
- 08-09-2011, 06:59 AM #15
- 08-09-2011, 07:04 AM #16
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
- 08-09-2011, 07:05 AM #17
Similar Threads
-
Converting if to Switch Case.
By king2be98 in forum New To JavaReplies: 5Last Post: 02-20-2011, 03:46 PM -
Switch Case statement
By seanfmglobal in forum New To JavaReplies: 7Last Post: 02-15-2011, 01:18 PM -
if else changes to switch-case?
By noobinoo in forum New To JavaReplies: 1Last Post: 04-23-2010, 05:56 PM -
[SOLVED] Is it possible to have If in a switch case?
By sfe23 in forum New To JavaReplies: 2Last Post: 02-23-2009, 12:34 AM -
Switch Case and Key Events
By AndrewM16921 in forum New To JavaReplies: 4Last Post: 01-26-2009, 11:20 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks