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 10 of 10

Thread: I'm wanting this code to jump out of the loop at case 2 of the switch statement

  1. #1
    Member
    Join Date
    Oct 2012
    Posts
    61
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default I'm wanting this code to jump out of the loop at case 2 of the switch statement

    For some reason it won't jump out of it.
      System.out.println("\nWould you like to try to guess the number again?\n" +
                                   "1.Yes\n" +
                                   "2.No\n" +
                                   "3.Exit the System");
                guessAgainPrompt=keyboard.nextInt();
     
                switch (guessAgainPrompt)
                {
                   case 1:
                   guessNumber();
                   guessCount=+1;
                   if (numberGuess==bonusNumber)
                   {
                      guessCorrect();
                      condition=false;
                   }
                   else
                   {
                      guessFail();
                   }
                   break;
     
                   case 2:
                   System.out.println("Thanks for playing the Game!");
                   condition=false;
                   break;
     
                   case 3:
                   System.out.println("Goodbye");
                   condition=false;
                   break;
                }
     
     
           }while (guessCount<=5 || condition==true);


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

    Default Re: I'm wanting this code to jump out of the loop at case 2 of the switch statement

    Can you explain what statement in the code is executing and not doing what you want it to do?

    What variables control the looping? Do they have the correct values for the loop to end?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Oct 2012
    Posts
    61
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: I'm wanting this code to jump out of the loop at case 2 of the switch statement

    Quote Originally Posted by Norm View Post
    Can you explain what statement in the code is executing and not doing what you want it to do?

    What variables control the looping? Do they have the correct values for the loop to end?
    Here's an update of the code:
    switch (guessAgainPrompt)
                {
                   case 1:
                   guessNumber();
                   guessCount=+1;
                   if (numberGuess==bonusNumber)
                   {
                      condition=false;
                      guessCorrect();
     
                   }
                   else
                   {
                      guessFail();
     
                   }
                   break;
     
                   case 2:
                   System.out.println("Thanks for playing the Game!");
                   condition=false;
                   break;
     
                   case 3:
                   System.out.println("Goodbye");
                   condition=false;
                   break;
                }
     
     
           }while (guessCount<=5 || condition==true);
          }

    The loop will not end here, therefore, I am prompted with the prompt that is at the beginning of the loop.
    if (numberGuess==bonusNumber)
                   {
                      condition=false;
                      guessCorrect();
     
                   }

    The variables that are controlling the loop is the 'condition variable, I have it initialised at the top as static boolean condition=true;
    The loop is set to continue while the condition is true, but because the above condition is false, shouldn't it jump out?

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

    Default Re: I'm wanting this code to jump out of the loop at case 2 of the switch statement

    variables that are controlling the loop is the 'condition variable
    Did you post the code you are working with? The while in the posted code:
     }while (guessCount<=5 || condition==true);
    has two variables the control the loop. If EITHER condition is true, the code will loop.
    What is the value of guessCount?

    Perhaps you want BOTH conditions to be true to continue the loop.
    If so, use an AND vs OR
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Oct 2012
    Posts
    61
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: I'm wanting this code to jump out of the loop at case 2 of the switch statement

    I want either of the variables to be true, but for now I just want to get the 'condition' one working. I can't understand why it won't jump out when the condition is false. Here is the full code:
     
     
    import java.util.Scanner;
     
    public class GuessNumber
     
    {
     
      static String Name;
      static  int guesses=0,bonusNumber = (int) (Math.random()*10) ,numberGuess, Score=0, guessAgainPrompt, guessCount;
      static  boolean condition=true;
     
      static  Scanner keyboard = new Scanner(System.in);
     
       public static void guessCorrect()
       {
          Score =+10;
          System.out.println("CONGRATULATIONS & WELL DONE!!!!\nYou have won the game.");
     
       }
     
       public static void guessFail()
       {
          System.out.println("Sorry, wrong number,");
     
       }
     
       public static void guessNumber()
       {
          System.out.println("Please guess a number between 1-10");
          numberGuess=keyboard.nextInt();
     
       }
     
     
       public static void main(String[] args)
       {                                                                                        //prompt for name to personalise
          System.out.println("What is your name?");
          Name=keyboard.nextLine();
     
          guessNumber();            //prompt for user to guess the bonus number
     
     
          if (numberGuess==bonusNumber)
          {
             guessCorrect();
             condition=false;
     
          }
          else
          {
             guessFail();
     
           do
           {
     
                System.out.println("\nWould you like to try to guess the number again?\n" +
                                   "1.Yes\n" +
                                   "2.No\n" +
                                   "3.Exit the System");
                guessAgainPrompt=keyboard.nextInt();
     
                switch (guessAgainPrompt)
                {
                   case 1:
                   guessNumber();
                   if (numberGuess==bonusNumber)
                   {
     
                      guessCorrect();
                      condition=false;
                   }
                   else
                   {
                      guessFail();
     
                   }
                   break;
     
                   case 2:
                   System.out.println("Thanks for playing the Game!");
                   condition=false;
                   break;
     
                   case 3:
                   System.out.println("Goodbye");
                   condition=false;
                   break;
                }
     
     
           }while (guessCount<=5 || condition==true);
          }
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
       }//main
    }//class

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

    Default Re: I'm wanting this code to jump out of the loop at case 2 of the switch statement

    Did you read the end of my last post?

    OR means if EITHER are true
    AND means BOTH must be true
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Oct 2012
    Posts
    61
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: I'm wanting this code to jump out of the loop at case 2 of the switch statement

    Quote Originally Posted by Norm View Post
    Did you read the end of my last post?

    OR means if EITHER are true
    AND means BOTH must be true
    Yeah, but I want OR because i just need either one to be true, || is the correct operator for OR right?

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

    Default Re: I'm wanting this code to jump out of the loop at case 2 of the switch statement

    i just need either one to be true
    If either are true then the loop continues looping. Is that what you want?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Oct 2012
    Posts
    61
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: I'm wanting this code to jump out of the loop at case 2 of the switch statement

    Its already set to true, if either are false, I want it to stop looping.

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

    Default Re: I'm wanting this code to jump out of the loop at case 2 of the switch statement

    if either are false, I want it to stop looping.
    Usimg OR says if either are true, keep looping

    Using AND says if both are true, keep looping. In other words if any are false stop looping.

    It seems like it would have been a very simple test to change the OR to AND and see what happened.
    If you don't understand my answer, don't ignore it, ask a question.

  11. The Following User Says Thank You to Norm For This Useful Post:

    javapol (February 24th, 2013)

Similar Threads

  1. How do I loop a switch Statement?
    By Arkeshen in forum Java Theory & Questions
    Replies: 10
    Last Post: August 2nd, 2018, 07:47 AM
  2. Need help with switch case
    By niko25 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 18th, 2012, 12:10 AM
  3. Do you add objects to an array when using switch statement with a For loop?
    By TheWhopper858 in forum Collections and Generics
    Replies: 2
    Last Post: November 13th, 2011, 01:28 PM
  4. Switch statement inside a for loop need help.
    By TheWhopper858 in forum Loops & Control Statements
    Replies: 1
    Last Post: November 12th, 2011, 11:50 PM
  5. wanting to convert printout to UPPER case
    By welikedogs in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 3rd, 2010, 01:22 PM