-2

I am trying to get my head around switch statements. I understand how to use a switch statement using a single variable but I cannot workout how to make the following code segment work:

If they cannot be done like this ( I struggled to find any info in google) could you please suggest a more effective way that would work if I wanted to add more cases which is why I am reluctant to use if statements.)

Edit: I'm not sure that my question was understood properly. I was wondering if it is possible to use the "And" and "OR" operators in a switch statement to make a switch statement that checks that two different variables match the requirements of each case. E.G Boy and girl are over 10, girl or boy are over 10, etc... I was asking for a Switch statement Solution specifically as this is more of a tutorial program than a useful program.

Also its really annoying getting my question marked down as being a duplicate when none of the so called "answers" that already exist answer my question and the many many unhelpful comments that ignore my question really suck.

enter code here

public class AgeCheck {
    public static void main(String args[]){
        int boy, girl;
        boy = 21;
        girl = 22;

        switch(boy & girl){
        case 1: boy > 10 && girl > 10;
        break;
        System.out.println("You can enter...");
        case 2: boy || girl < 10;
        System.out.println("You cannot enter, someone amongst you is unworthy...");
        break;
        default:
            System.out.println("Your age is unknown...");
        }
    }

}
mBaffle
  • 61
  • 1
  • 7
  • 5
    Why don't you just use if statements? This isn't a job for a switch statement. – Kevin Workman Jul 03 '14 at 13:13
  • `I wanted to add more cases which is why I am reluctant to use if statements.` which are the cases you're gonna add? In this example I don't find more ways program can go through. – Frakcool Jul 03 '14 at 13:19
  • 1
    Keep in mind that `boy & girl` resolves to `20`, so it will always go to the default case – Absurd-Mind Jul 03 '14 at 13:20
  • @KevinWorkman Because I want to find a solution using switch statements to expand my knowledge of Java. – mBaffle Jul 03 '14 at 14:38
  • @Absurd-Mind Thank you for sharing some useful info, I didn't know it would just add the variables every time :) – mBaffle Jul 03 '14 at 14:39
  • 1
    This isn't a job for a switch statement. If you want to expand your knowledge of Java, learn that different things have different jobs. Use the correct thing for the job at hand. – Kevin Workman Jul 03 '14 at 14:40
  • @Kevin Workman The way I learn is that I choose a concept, then I play around with the concept to see what I can do with it and can't do with it and then I understand the concept in a complete way. This is what works best for me. – mBaffle Jul 03 '14 at 14:44
  • 1
    @mBaffle I didn't mean to insult you... I just wanted to point out that from what I see in your code I think you're not really getting how to use switch statements... Sorry for being harsh and short... – jamp Jul 03 '14 at 14:45
  • Okay, and understanding the concept in a complete way here means realizing that this isn't a job for a switch statement. It's also not a job for a for loop or a recursive function. You can't just choose to use something and expect it to work for you. Part of understanding something is understanding when **not** to use it. – Kevin Workman Jul 03 '14 at 14:47
  • @Kevin I'm not interested in the best time to use things currently I can work that out later I just wanted to know if it the and/or operators could be used in a switch statement with an example of how they might be used. I can now infer that you can't use and/or statements but telling me not to use switch statements doesn't help my understanding. – mBaffle Jul 03 '14 at 14:57
  • I'm not sure how telling you that you can't do this doesn't help you learn that you can't do this, but okie dokie. – Kevin Workman Jul 03 '14 at 15:00
  • @Kevin its OK we are on totally different wavelengths. You think I am after the best way to code what I coded and I am after a way to do what I am doing using switch statements. It's just annoying having people comment your question down so much when they don't understand the question... – mBaffle Jul 03 '14 at 15:10
  • We understand your question. The answer to your question is to use if statements. You don't understand that these answers are, in fact, the answer. – Kevin Workman Jul 03 '14 at 15:13
  • So why didn't you just say that it cannot be done in the first place? - – mBaffle Jul 03 '14 at 15:19
  • Since you asked in another question: I assume you got downvoted mainly because of the lack of research before asking and the (somewhat unduly long) question. You could have googled "java switch syntax", you could have searched SO, but instead you posted a question wasting people's time. The linked questions **do** answer yours. They are about the syntax of the Java `switch` command. We **are** nice people, just make sure you convince us that you've tried to solve your problem before posting. In this case, read a book about Java basics. Hope you stick around! – kraxor Jul 04 '14 at 08:16
  • @kraxor I spent 40 minutes googling and looking at the java documentation which did not mention anything to do with what I was looking for. Its a lot easier to just google than writing out a question, making an example to dmeonstrate then waiting for an answer. Instead I got links to irrelevant things, had 7 answers linked to me as alternatives which again had nothing to do with what I was asking and then lost all of my rep. HOw am I supposed to find this site useful at all if I need rep to do anything and I just get downvoted due to irrelevant questions having been already asked? – mBaffle Jul 04 '14 at 13:09

2 Answers2

4

You can't use boolean operators with switch statements. What you can do is something like this:

case 1: case 2:
    // do some stuff
    break;
jhobbie
  • 1,016
  • 9
  • 18
  • So like: Case 1: boy < 10; Case 2: girl < 10; /*executed code*/ break; ? – mBaffle Jul 03 '14 at 14:40
  • Hmm, I think you don't understand how `switch` statements work very well. Most people here are right, you do want an `if` statement. However, you should still look at how switch statements work here: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html – jhobbie Jul 03 '14 at 14:52
  • I already read that article before posting this. It didn't mention anything about using more than one variable at a time so I wondered if anybody on here knew a way. The reason everybody keeps saying use an if statement is because I pretty much converted my if statement code to switch statement code so it naturally looks like it belongs in an if statement. – mBaffle Jul 03 '14 at 15:08
  • I just mean that what you really should say is `case boy<10: case girl<10: //code; break;`. You aren't supposed to number your cases. – jhobbie Jul 03 '14 at 15:09
  • Thank you muchly for your insight you were the only helpful person to input on this question. I wish I could mark this as the answer but I have lost too much rep from this question to be able to do that. – mBaffle Jul 03 '14 at 15:31
2

You need not to have switch here and the way you wrote is not correct usage of switch.

You need a proper if else

if (boy > 10 && girl > 10) {
        System.out.println("You can enter...");
    } else if (boy < 10 || girl < 10) {
        System.out.println("You cannot enter, s...");
    } else {
        System.out.println("Your age is unknown...");
    }

Clear. Right ?

Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307