Results 1 to 10 of 10
  1. #1
    bdl1127 is offline Member
    Join Date
    Jan 2012
    Posts
    26
    Rep Power
    0

    Default How do I make this case execute correctly in switch statement?

    I have the switch statement working nicely im just having a hard time figuring out how to get this code to do what I want. When I run it now it just prints one time. How would I get this to execute until I get a winning match and how would I create a counter to know how many times it took?

    case 2: menu = "Spin wheels until a winning match. Find out how many times it took to win";


    do{
    one = gen.nextInt(5) + 1;
    two = gen.nextInt(5) + 1;
    three = gen.nextInt(5) + 1;
    } while (one != two && two != three);




    System.out.println(+ one + " " + two + " " + three);


    break;

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: How do I make this case execute correctly in switch statement?

    how would I create a counter to know how many times it took?
    Define a int variable outside the loop, and increment it inside the loop.

    When I run it now it just prints one time.
    There is nothing in the code that would make it print more than one time per execution of the switch statement with a value of 2.
    Why do you expect it to print more than once?

  3. #3
    bdl1127 is offline Member
    Join Date
    Jan 2012
    Posts
    26
    Rep Power
    0

    Default Re: How do I make this case execute correctly in switch statement?

    I know theres not. Im supposed to create a slot machine. I need that loop to repeat itself and print all the numbers until they all equal each other.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: How do I make this case execute correctly in switch statement?

    If you want to print all the numbers, move the println statement inside of the loop.

  5. #5
    bdl1127 is offline Member
    Join Date
    Jan 2012
    Posts
    26
    Rep Power
    0

    Default Re: How do I make this case execute correctly in switch statement?

    ok i did that but its still not executing like i want it to. I want it to continue to print the numbers until they all match up

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: How do I make this case execute correctly in switch statement?

    The println inside of the loop will print one time for every time around the loop.
    Can you post the output from your program where it does not continue printing every time around?

    How do you want it to print?

  7. #7
    bdl1127 is offline Member
    Join Date
    Jan 2012
    Posts
    26
    Rep Power
    0

    Default Re: How do I make this case execute correctly in switch statement?

    this is my code:

    case 2: menu = "Spin wheels until a winning match. Find out how many times it took to win";


    do{
    one = gen.nextInt(5) + 1;
    two = gen.nextInt(5) + 1;
    three = gen.nextInt(5) + 1;
    System.out.println(+ one + " " + two + " " + three);
    } while (one != two && two != three);



    break;

    I want it to print out until all the integers match up. IE

    013
    253
    124
    254
    555

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: How do I make this case execute correctly in switch statement?

    What does your program print out now?
    Since it is using random numbers, every execution will generate a different group of numbers.

    How are you defining the gen variable?

  9. #9
    bdl1127 is offline Member
    Join Date
    Jan 2012
    Posts
    26
    Rep Power
    0

    Default Re: How do I make this case execute correctly in switch statement?

    heres the whole statement with the gen variables:

    String menu = " ";
    switch (option){
    case 1: menu = "Pull handle once and print results";

    one = gen.nextInt(5) + 1;
    two = gen.nextInt(5) + 1;
    three = gen.nextInt(5) + 1;
    System.out.println(+ one + " " + two + " " + three);

    if (one == two && two == three) {
    System.out.println("Winner!");
    }
    break;

    case 2: menu = "Spin wheels until a winning match. Find out how many times it took to win";


    do{
    one = gen.nextInt(5) + 1;
    two = gen.nextInt(5) + 1;
    three = gen.nextInt(5) + 1;
    System.out.println(+ one + " " + two + " " + three);
    } while (one != two && two != three);



    break;
    case 3: menu = "Quitter! Goodbye!";
    break;
    }

    this is what it prints out currently after selecting option 2.

    3 1 3
    2 2 3
    Please select an option (1 or 2 or 3):
    1: Pull handle once and print results
    2: Spin wheels until a winning match. Find out how many times it took to win
    3: Quit

    It prints something different everytime but it doesnt continue to print till all the numbers match

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: How do I make this case execute correctly in switch statement?

    but it doesnt continue to print till all the numbers match
    Look at the condition that controls the loop. With an AND relationship both of the conditions must be true for the whole expression to be true. If one is true and the other false, the the whole expression is false.

    You need a new condition to test to keep the loop going.

Similar Threads

  1. Using a switch/case with my if statements
    By coding in forum New To Java
    Replies: 2
    Last Post: 03-07-2011, 08:01 AM
  2. Switch Case statement
    By seanfmglobal in forum New To Java
    Replies: 7
    Last Post: 02-15-2011, 01:18 PM
  3. if else changes to switch-case?
    By noobinoo in forum New To Java
    Replies: 1
    Last Post: 04-23-2010, 05:56 PM
  4. Replies: 2
    Last Post: 02-23-2009, 12:34 AM
  5. Switch Case and Key Events
    By AndrewM16921 in forum New To Java
    Replies: 4
    Last Post: 01-26-2009, 11:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •