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

Thread: do while loop with nested while question

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    6
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Question do while loop with nested while question

    hey guys here is a program i'm working on. I cant get it to count valid guesses 1-20 it only counts invalid and for some reason on my net beans ide it never say build successful even when the random number is guessed and the you won is displayed. any thoughts are very appreciated.20101005173749.jpg

    20101005173814.jpg


  2. #2
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: do while loop with nested while question

    It would be alot easier to help you if you posted all of your code, and surround it in [highlight=java][/highlight]. I have no clue on the netbeans problem. I strongly dislike NB myself.

  3. The Following User Says Thank You to Brt93yoda For This Useful Post:

    johneusmc (October 5th, 2010)

  4. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: do while loop with nested while question

    Please copy and paste the code into a post directly (remember to use the code tags). As is it is difficult to give much guidance

  5. The Following User Says Thank You to copeg For This Useful Post:

    johneusmc (October 5th, 2010)

  6. #4
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: do while loop with nested while question

    Quote Originally Posted by copeg View Post
    Please copy and paste the code into a post directly (remember to use the code tags). As is it is difficult to give much guidance
    Gotcha Copeg

  7. #5
    Junior Member
    Join Date
    Oct 2010
    Posts
    6
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: do while loop with nested while question

    hey guys thanks for the info I cant figure out to convert the source file into someting i can attach so i'll just write it out. the program prompt users for a number between 1 and 20 then generates a random number. only ints entered in the range of 1 to 20 are supposed to count as guesses here goes.

    import java.util.Scanner;
    import java.util.Random
     
    public class labAssign5 {
     
    public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);
    int rNumber, userGuess, totalGuesses;
    Random rGenerate = new Random( );
    rNumber = rGenerate.nextInt(20) + 1;
    totalGuesses = 0
     
    do
    {System.out.print("Enter a number 1 to 20 -> ");
    userGuess = keyboard.nextInt( );
     
     {
         while (userGuess<= 20 && userGuess >=1 && userGuess != rNumber)
          { System.out.print("Try again.\nEnter a number 1 to 20 -> ";
             userGuess = keyboard.nextInt( );
           }
     
             totalGuesses ++;
     
             if (userGuess == rNumber)
                {
                   System.out.print("You won!\nThe number of guesses is " + totalGuesses);
                    userGuess = keyboard.nextInt( );
                 }
             }
         } while (userGuess <1 || userGuess >20);
     
    System.exit(0);
     }
    }

  8. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: do while loop with nested while question

    Gotcha Copeg
    Just this once

    johneusmc, check the logic carefully. (if I understand the goal of the code...) Are you truly incrementing totalGuesses for each attempt? Place some println's to debug the code and you can see if userGuess gets incremented and where.

  9. The Following User Says Thank You to copeg For This Useful Post:

    johneusmc (October 5th, 2010)

  10. #7
    Junior Member
    Join Date
    Oct 2010
    Posts
    6
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: do while loop with nested while question

    okay thanks for the help copeg. if anyone is familiar with netBeans can u tell me why it keeps running until i press enter then on the blank line put in any number and press enter then it stops running and says build successful. There are no syntax errors.
    /*This program prompts the user for a number 1-20 and compares it with a
     * randomly generated number. If it matches the program displays you won.
     * if not, it displays try again. The program also displays the number of
     * guesses. It also does not count invalid entries as guesses no matter where in
     *  the program they are entered.
     */
    import java.util.Scanner;
    import java.util.Random;
    /**
     *
     * @author John LaFave
     */
    public class labAssign5 {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            Scanner keyboard = new Scanner(System.in);
            int rNumber, userGuess;
            int totalGuesses;
            Random rGenerate = new Random();
            rNumber = rGenerate.nextInt(20) + 1;
            totalGuesses = 0;
     
     
     
     
            do
            {
                System.out.print("Enter a number 1 to 20 -> ");
                userGuess = keyboard.nextInt();
     
                {
                    while (userGuess <=20 && userGuess >=1 && userGuess != rNumber)
                    {
                        System.out.print("Try again.\nEnter a number"
                    + " 1 to 20 -> "); userGuess = keyboard.nextInt();
     
                            totalGuesses ++;
                    }
     
     
                            if (userGuess == rNumber)
                        {
                           totalGuesses ++;
                         System.out.print("You won!\nThe number of guesses is "
                         + totalGuesses);
                         userGuess = keyboard.nextInt();
     
                        }
     
     
                }
     
            } while (userGuess < 1 || userGuess > 20);
     
           System.exit(0);
       } }
    Last edited by helloworld922; October 5th, 2010 at 09:37 PM.

  11. #8
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: do while loop with nested while question

    Likely it's a problem with your timing and what order you're doing stuff in. Save your work, then close netbeans. This will effectively terminate any programs NetBeans could be running/building. Then re-start netbeans. Click the build button once. Be patient, it may take a while for it to finish building. Once it tells you that it's finished building, click the Run button once. Again, it may take a while for the program to start up. Also, once you're done, make sure your program has terminated (or forcibly terminate it). This will prevent any weird things from happening such as accidentally inputting data to the old program.

  12. The Following User Says Thank You to helloworld922 For This Useful Post:

    johneusmc (October 5th, 2010)

  13. #9
    Junior Member
    Join Date
    Oct 2010
    Posts
    6
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: do while loop w/ nested while. how to forces loop stop w/o break or sentinal valu

    okay I figured out that my outer do-while loop wants to keep running when I want the program to end after my nested while loop gets the random number match. I can terimnate the program by entering an int into the console that makes the do-while false but that doesn't really work for an efficient program. I tried using break but it either stops the whole thing or does notjing depending on placement. Any thoughts would be awesome.oh and I tried nesting the do-while inside the while but couldn't get it to work. does anyone think thats the way to go or the force stop?
    Last edited by johneusmc; October 6th, 2010 at 09:32 AM. Reason: forgot

  14. #10
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: do while loop with nested while question

    You had a put System.in during your if loop, not sure why. Try this

    /*This program prompts the user for a number 1-20 and compares it with a
     * randomly generated number. If it matches the program displays you won.
     * if not, it displays try again. The program also displays the number of
     * guesses. It also does not count invalid entries as guesses no matter where in
     *  the program they are entered.
     */
    import java.util.Scanner;
    import java.util.Random;
    /**
     *
     * @author John LaFave
     */
    public class labAssign5 {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            Scanner keyboard = new Scanner(System.in);
            int rNumber, userGuess;
            int totalGuesses;
            Random rGenerate = new Random();
            rNumber = rGenerate.nextInt(20) + 1;
            totalGuesses = 0;
     
     
     
     
            do
            {
                System.out.print("Enter a number 1 to 20 -> ");
                userGuess = keyboard.nextInt();
     
                {
                    while (userGuess <=20 && userGuess >=1 && userGuess != rNumber)
                    {
                        System.out.print("Try again.\n" + 
                       "Enter a number 1 to 20 -> ");  userGuess = keyboard.nextInt();
     
                            totalGuesses ++;
                    }
     
     
                            if (userGuess == rNumber)
                        {
                           totalGuesses ++;
                         System.out.print("You won!\nThe number of guesses is " + totalGuesses);
                         System.out.println("["+userGuess+"]" + "["+rNumber+"]");
     
                        }
     
     
                }
     
            } while (userGuess < 1 || userGuess > 20);
     
           System.exit(0);
       } }

    The point of
     System.out.println("["+userGuess+"]" + "["+rNumber+"]");
    is to test if those are even, if they are and it still doesn't end the while statement than I have no clue.
    It will look funny, but if it ends like it should, just delete that line. I added my own style, you can change it if you want, shouldn't make a difference.
    Also for testing purposes you could add rNumber = 1 and see if it ends like it should without guessing the number.
    Last edited by Tjstretch; October 6th, 2010 at 02:56 PM.

  15. The Following User Says Thank You to Tjstretch For This Useful Post:

    johneusmc (October 6th, 2010)

  16. #11
    Junior Member
    Join Date
    Oct 2010
    Posts
    6
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: do while loop with nested while question

    Thanks a lot. That did it. appreciate the help.

Similar Threads

  1. nested loop
    By b109 in forum Java Theory & Questions
    Replies: 1
    Last Post: May 30th, 2010, 10:05 AM
  2. Nested try blocks
    By Ahmed. in forum Member Introductions
    Replies: 2
    Last Post: April 30th, 2010, 08:12 AM
  3. Equilateral Triangle Using nested for loop
    By uchizenmaru in forum Loops & Control Statements
    Replies: 2
    Last Post: January 14th, 2010, 10:01 PM
  4. how do i draw a shape with nested loops?
    By Kilowog in forum Loops & Control Statements
    Replies: 1
    Last Post: September 25th, 2009, 12:14 AM
  5. [SOLVED] Trouble with draw and fillRect in pyramid logic using nested loop
    By LiquidMetal in forum Loops & Control Statements
    Replies: 4
    Last Post: April 27th, 2009, 03:25 AM