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: While loop question

  1. #1
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default While loop question

    Could anyone help me figure out why the outer loop stop running after just trial 1? Thanks
            System.out.print("Please enter the number of trials: ");
            int uInput = userInput.nextInt();
     
            for(int counter = 1; counter <= uInput; counter++)
            {
            while(randNum1 != 1)
            {
            randNum1 = randNum.nextInt(5);    
     
            if(randNum1 == 1)
               { 
                wins++;   
                bottleCapNum++;
            }
            else
            bottleCapNum++;
            }
                }


  2. #2
    Member
    Join Date
    Jul 2012
    Posts
    71
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Default Re: While loop question

    u have in your while loop randNum1 != 1 but you also have if(randNum1 == 1) inside the loop so if that case is true then after that loop randNum1 will be equal to 1 so your while loop will never run again because the condition is now false...

  3. #3
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: While loop question

    Oh! Thank you for that pointer. Do you have any suggestion on how I can modify my code so that the outer loop will run depending on the user input of trials and the inner loop search until it find the number "1" out of the 0-4 random number that is generated each trials? Then I would need a conditional statement where if the number is 1, the wins++, if it not 1, then bottleCapNum++.

  4. #4
    Member
    Join Date
    Jul 2012
    Posts
    71
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Default Re: While loop question

    i think i get what ur trying to do... just make the randNum1 generate without the while loop and see if that's what u had in mind... the use of that while loop makes possible for complications and since you're only checking for one condition the if else statement will be all u need really

  5. #5
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: While loop question

    After the random number is generated, I need to compare it to see if the number is =1, if not the number would have to generate again, for a total of 5 time. Without the while loop, is there another method of doing this ? Thanks

    ** Better yet, for my while loop, do you know any way I can change the (randNum1 !=1) to something else that does not conflict with the conditionals statement but still have the while loop run until it get 1?

  6. #6
    Member
    Join Date
    Jul 2012
    Posts
    71
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Default Re: While loop question

    ok i get it you want the number to generate a certain amount of times in the case that the number isn't one and keep count of other variables while that condition is true or not... just reset the randNum1 variable after the while loop so that way when the for loop iterates again the while statement will always be true at least once

  7. #7
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: While loop question

    May I know how to reset a variable? Thank you.

  8. #8
    Member
    Join Date
    Jul 2012
    Posts
    71
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Default Re: While loop question

    make it equal 0 after the while loop

  9. The Following User Says Thank You to C++kingKnowledge For This Useful Post:

    maple1100 (December 25th, 2012)

  10. #9
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: While loop question

    Thanks for your help, King.

  11. #10
    Member
    Join Date
    Jul 2012
    Posts
    71
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Default Re: While loop question

    no prob i'm just like u trying to gain knowledge and help others.... happy coding

  12. The Following User Says Thank You to C++kingKnowledge For This Useful Post:

    maple1100 (December 25th, 2012)

Similar Threads

  1. Jagged array loop question
    By Dankaru in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 14th, 2012, 08:21 AM
  2. While Loop quick question
    By loui345 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 1st, 2012, 11:46 PM
  3. Loop Question - Very new beginner
    By Callcollect in forum Loops & Control Statements
    Replies: 12
    Last Post: January 18th, 2012, 03:01 AM
  4. [SOLVED] VERY basic question - if loop
    By kobi1988 in forum Loops & Control Statements
    Replies: 3
    Last Post: October 12th, 2011, 06:34 PM
  5. do while loop with nested while question
    By johneusmc in forum What's Wrong With My Code?
    Replies: 10
    Last Post: October 6th, 2010, 04:45 PM