-1

Im starting to learn java and im trying out switch statements. Long story short my question is are able to put a condition in a switch statement? Below is what im trying to do

   System.out.print("Enter credits: ");
   credits = in.nextInt();

   switch(credits){
   case (credits > 12):
     //do something
   break;

   case (credits < 12):
     //do something
   break;

   default:
     //do something
   break;

im getting errors in my compiler so im assuming you cant, but maybe im just doing it wrong, or theres just another syntax to do it? Any help is appreciated. Thanks!

malone
  • 11
  • 1
    See Java's [switch statement](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html). – jarmod Feb 11 '21 at 00:28
  • Advice: Don't try to learn a programming language by trial and error. Read (or at least check) a good tutorial or textbook instead. If there are clever alternative syntaxes ... the tutorial will tell you! – Stephen C Feb 11 '21 at 00:38
  • Welcome to SO! All science is made by trial-and-error, so props to you for thinking like a scientist, and not like a professional-student. – HoldOffHunger Feb 11 '21 at 00:42
  • Umm ... but you don't (say) learn basic chemistry by going into a chem lab and mixing chemicals at random to see what happens. You read the text book, unless you want to loose fingers, eyes, etc. – Stephen C Feb 11 '21 at 01:00
  • Anyway, here's a dup link: https://stackoverflow.com/questions/31497818 – Stephen C Feb 11 '21 at 01:02

1 Answers1

0

Case statements are for static equality-only comparisons on the switch argument.

For inequalities, use a standard if-else

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245