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

Thread: do-while loop won't work

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default do-while loop won't work

    can any1 please tell me why the do-while loop wont work? im trying to use it to validate my program.
    anytime a month appears it lets me enter any value i want when i want it to go back to the beginning if its out of the range of 0-744
    please help :S

    static void enterValues (double [] sunshine)
    {
    //create a Scanner object to read input
    Scanner keyboard=new Scanner (System.in);

    //Declare variable for loop counter and initialize
    for (int count = 0; count < 12; count++)

    do
    {
    //Prompt and input hours of sunshine for each month
    System.out.print("Enter hours of sunshine for "+month[count]+" " );
    sunshine [count] = keyboard.nextDouble();
    }
    while (sunshine[count]>0 && sunshine[count]<744);
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,198
    Thanks
    49
    Thanked 2,173 Times in 2,146 Posts

    Default Re: do-while loop won't work

    why the do-while loop wont work?
    Please explain what happens?
    Does it loop when it should not loop?
    Does it not loop when it should loop?
    Try debugging the code by printing out the values of the variables used to control the loop:
    count and sunshine[count] so you can see what the computer sees. That will help you understand why the code does what it does.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Image won't work!
    By mkrage in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 26th, 2012, 05:08 PM
  2. Why won't this for loop work?
    By vwillis in forum Loops & Control Statements
    Replies: 1
    Last Post: October 14th, 2011, 12:49 PM
  3. MergeSort won't work
    By joshft91 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 7th, 2011, 08:44 PM
  4. My lines won't work!!!
    By The Mewzytion in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 2nd, 2010, 10:24 AM
  5. Why won't this while loop work?
    By trueblue in forum Loops & Control Statements
    Replies: 2
    Last Post: July 17th, 2009, 09:10 AM