Results 1 to 8 of 8
Thread: switch statements & conditionals
- 09-24-2011, 09:50 PM #1
Member
- Join Date
- Sep 2011
- Posts
- 9
- Rep Power
- 0
-
Re: switch statements & conditionals
Try it and let's see what the compiler has to say about it.
- 09-24-2011, 10:16 PM #3
Member
- Join Date
- Sep 2011
- Posts
- 9
- Rep Power
- 0
Re: switch statements & conditionals
Heh, you have a point. I don't know why I haven't tried it yet.
I've tried googling it and looked through two java books, couldn't find anything on this.
javac "test.java" (in directory: C:\Users\linkxs\Desktop\tempjava)Java Code:import java.util.*; public class test { public static void main(String[] args) { int n =0; Scanner kbd = new Scanner(System.in); n = kbd.nextInt(); switch (n) { case (n<10): System.out.println("less than 10"); break; case n>10: System.out.println("more than 10"); break; } } }
test.java:13: incompatible types
found : boolean
required: int
case (n<10):
^
test.java:16: incompatible types
found : boolean
required: int
case n>10:
^
I'll take that as a no.
-
Re: switch statements & conditionals
I agree with your take on it. Also when in doubt, check out what the Java Language Specification or JLS has to say about this: JLS 14.11 The Switch Statement.
It will tell you that case must be followed by a constant expression or an enum.
- 09-25-2011, 12:13 AM #5
Member
- Join Date
- Sep 2011
- Posts
- 9
- Rep Power
- 0
Re: switch statements & conditionals
Damn, I really wish that would be possible. Otherwise I have to do a ton of branching ifs..
- 09-25-2011, 03:12 AM #6
Member
- Join Date
- Sep 2011
- Posts
- 14
- Rep Power
- 0
Re: switch statements & conditionals
Basicially a switch lets you choice, so if you want n>10 and n<10 and n=10 (because of default), well you have to declare all the choices. At least if I'm not mistaken.
However if I'm correct you could use this
Then again if your work with more and larger n's it gets really branched.Java Code:public class test { public static void main(String[] args) { Scanner kbd = new Scanner(System.in); int n = kbd.nextInt(); if (n>10) { System.out.println("Number is more then 10 "); } else //n<=10 { System.out.println("Number is less than 10 or equal to 10"); } } }Last edited by Aero; 09-25-2011 at 03:25 AM. Reason: Addition
- 09-25-2011, 05:27 AM #7
Member
- Join Date
- Sep 2011
- Posts
- 9
- Rep Power
- 0
Re: switch statements & conditionals
Yeah, exactly, that's the issue I was having: I had a ton of branched if's, and I thought I'd put switch in there instead, but then it becomes even longer.
- 09-25-2011, 06:17 AM #8
Similar Threads
-
switch statements
By jim01 in forum New To JavaReplies: 7Last Post: 04-10-2011, 10:52 PM -
Using a switch/case with my if statements
By coding in forum New To JavaReplies: 2Last Post: 03-07-2011, 08:01 AM -
switch
By dj kourampies in forum New To JavaReplies: 17Last Post: 01-30-2009, 05:32 PM -
[SOLVED] Using dialog boxes and switch statements question
By hungdukie in forum New To JavaReplies: 2Last Post: 11-22-2008, 05:30 AM -
Do this without conditionals...
By Arez in forum New To JavaReplies: 4Last Post: 10-06-2008, 02:15 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks