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.


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

Results 1 to 2 of 2

Thread: Switch Statement

  1. #1
    Junior Member
    Join Date
    Oct 2018
    Thanked 0 Times in 0 Posts

    Default Switch Statement

    Hello, Im new here to forums and learning Java. I had a test that had this type of code as an example, I had to write it from memory. My issue is it ask if the variable vacation=2 or 3 what would display. I know the answers now but got one wrong. I need it to display the output 6 and 10. I remember that at certain case there were no break. I think it was after case1 and case 4 there weren't any break that I can remember. The code is to display the sum of both variables show and boat. Basically what needs to change or taken out to display 6 or 10 when I change the assignment for vacation from 2 to 3...would appreciate the help. I think I need to erase case 5 but either way its not working so it can display 6...

    public class SwitchStatement
    public static void main(String[] args)
    int vacation = 3;
    int boat = 1, show = 10;

    switch (vacation){ //if there are no break the answer is default

    case 1:
    show = 1;
    boat = 1;
    case 2:
    show = 2;
    boat = 2;
    case 3:
    show = 3;
    boat = 3;
    case 4:
    show = 4;
    boat = 4;
    case 5:
    show = 5;
    boat = 5;

    System.out.println("The answer is" + " " + (show + boat));
    Last edited by Niko; October 24th, 2018 at 05:57 PM.

  2. #2
    Join Date
    Sep 2018
    My Mood
    Thanked 38 Times in 36 Posts

    Default Re: Switch Statement

    Any attempt to provide a solution would probably be different than what was on the test. But if the values are 2 and 3 I can't see it working the way you have cases 2 and 3 and printing out 6 and 10. And normally, the default is the last statement and the print statement would be outside the switch block. If you can get the original code we can help you understand it.


Similar Threads

  1. Need help with my switch statement
    By PROTO in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 27th, 2017, 07:33 AM
  2. How to turn my If statement into a case/switch statement?
    By blobby404 in forum What's Wrong With My Code?
    Replies: 23
    Last Post: June 19th, 2014, 03:11 PM
  3. [SOLVED] A Loop statement and a switch statement issue
    By sternfox in forum Loops & Control Statements
    Replies: 13
    Last Post: March 7th, 2013, 04:19 PM
  4. Replacing an If statement with a Switch statement
    By logi in forum Loops & Control Statements
    Replies: 9
    Last Post: February 4th, 2013, 12:21 AM
  5. help with switch statement
    By robertsbd in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 12th, 2010, 12:52 PM

Tags for this Thread