Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Having trouble with switch statements for a school work

  1. #1
    Junior Member
    Join Date
    Nov 2021
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Having trouble with switch statements for a school work

    We were asked to write a program that accepts 5 integers, select an arithmetic operation for odd and even, and perform the corresponding operation.
    Ive just been stuck on how I can make it that the inputs would be detected as odd or even AND then do the selected operation seperately for odd and even. This topic makes us focus on switch statements.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,147
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Having trouble with switch statements for a school work

    how I can make it that the inputs would be detected as odd or even
    Test the lower order bit of an int to determine if it is odd or even. If the bit is 1, it is odd. Use the AND operator (&) with a 1.
    For example:
          System.out.println("odd " + (3 & 1) + ", even " + (4 & 1)); //  odd 1, even 0
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2021
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Having trouble with switch statements for a school work

    I need the actual values of the odd and even numbers to be added or substracted seperately, Im just really stuck on how could I do that.
    .

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,147
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Having trouble with switch statements for a school work

    I need the actual values of the odd and even numbers
    Where does the program get its input from? Are they entered by a user?
    The code I posted shows a way to determine if a number is even or odd.
    Here is another example:
          int val = 4;    // the number to test
          System.out.println((val & 1) == 1 ? "is odd" : "is even");
    The program would get a number from the user, test if it is even or odd and process accordingly:
    Get number from user
    if an even number
    process even number
    else process odd number
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] My fleet of squares doesnt work (simple high school work)
    By macko939 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: February 20th, 2014, 02:56 PM
  2. Im very new to java And switch statements
    By RevChe in forum Loops & Control Statements
    Replies: 9
    Last Post: March 17th, 2013, 10:14 AM
  3. IF and SWITCH Statements: How and When to Use Them
    By snowguy13 in forum Java Programming Tutorials
    Replies: 2
    Last Post: January 11th, 2012, 10:46 AM
  4. Help with switch statements
    By suxen in forum Loops & Control Statements
    Replies: 4
    Last Post: February 15th, 2011, 04:55 AM
  5. need help with JButton and switch statements
    By jjoubert in forum AWT / Java Swing
    Replies: 5
    Last Post: October 28th, 2009, 09:13 AM

Tags for this Thread