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

Thread: Question on do-while loops

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Question on do-while loops

    Hi, I'm currently trying to sort out a few problems with a program I'm trying to write. I'm pretty new to this, so the solution is probably going to be obvious, but here goes...

    I have two seperate do-while loops (though they are very similar to each other) and which of the loops that is run depends on a boolean input from the user. The amount of times the loops is run depends on an int input from the user. Here's the problem I'm having - if I put the scanner for the int input (for the number of times the loop is run) inside the loop, then it works fine, however, the user keeps getting asked how many times the loops will be run over and over again for as many times as the loop is run, obviously because that code is inside the loop. If I put that code outside of the two loops, then the loops won't run more than once.

    Here's the relevant code. I tried to edit it down because the program is quite long, but if I've left out anything important let me know.


    boolean boolean1;

    int innings = 0;

    int innings2 = 0;

    int numberofinnings;

    Scanner sc;

    sc = new Scanner(System.in);


    System.out.println("Will player one be batting first? (true/false)");
    boolean1 = sc.nextBoolean();

    System.out.println("How many innings will each player have?");
    numberofinnings = sc.nextInt();


    if (boolean1==true)

    {

    do

    {
    //More code here

    innings++;
    }

    while (innings < numberofinnings);
    }


    else if (boolean1==false)

    {

    do

    {
    //More code here

    innings2++;
    }

    while (innings2 < numberofinnings);
    }



    I edited out the entire middle sections of the loops because they were very long I didn't think they were relevant, I'm pretty sure I didn't change anything in there between the loops working and the loops not working.

    If I put the "How many innings will each player have?" part inside the two loops (once in both of them) then the loops work, but like I mentioned above, the code obviously keeps repeatedly asking how many times the loop will be run, because that part of the code is inside the loop. Why does it do this? How can I initialize the variable "numberofinnings" outside of the loops and then have them run the number of times that is entered into that variable?
    Last edited by Sean448; April 22nd, 2012 at 04:04 AM.


  2. #2
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Question on do-while loops

    Quote Originally Posted by Sean448 View Post
    If I put the "How many innings will each player have?" part inside the two loops (once in both of them) then the loops work, but like I mentioned above, the code obviously keeps repeatedly asking how many times the loop will be run, because that part of the code is inside the loop. Why does it do this? How can I initialize the variable "numberofinnings" outside of the loops and then have them run the number of times that is entered into that variable?
    If you want all this run for several times (for the number of players, if I'm getting it right) you can put the questions and the two do...while loops inside another do...while loop (that will run for the number of players).

  3. #3
    Member
    Join Date
    Jan 2012
    Posts
    33
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Question on do-while loops

    Looks very similar to this post
    http://www.javaprogrammingforums.com...dom-class.html
    So your re-writing it? Why did you create a new account?
    If you want it to start over and ask the initializing questions then just put in 2 while loops
    while(running)
    {
          //write initializing questions
          while(game_is_on)
          { 
                //gamecode
           }
           // ask if you want to exit
    }
    Dont really understand your question, just post the full source

Similar Threads

  1. Beginner question about for loops...
    By ninjaBob in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 29th, 2012, 03:57 PM
  2. Loops & Series Question
    By nuttaay in forum Loops & Control Statements
    Replies: 5
    Last Post: November 29th, 2011, 01:46 PM
  3. For loops
    By JCTexas in forum Loops & Control Statements
    Replies: 4
    Last Post: September 21st, 2011, 05:43 PM
  4. help with loops... :(
    By Macgrubber in forum Loops & Control Statements
    Replies: 2
    Last Post: November 2nd, 2010, 12:38 PM
  5. need help with loops plz
    By Kilowog in forum Loops & Control Statements
    Replies: 4
    Last Post: September 28th, 2009, 08:11 AM