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.

Page 2 of 2 FirstFirst 12
Results 26 to 30 of 30

Thread: Do While Loop

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

    Default Re: Do While Loop

    Now it's telling me that highestDonation isnt initialised. I don't know how to intitialise this variable, I know how, but don't know what I should be setting it to.

  2. #27
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,138
    Thanks
    48
    Thanked 2,156 Times in 2,129 Posts

    Default Re: Do While Loop

    Do the same as these:
     ...donationQuantity=0, maxDonation=0 ...
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Do While Loop

    Here is my code now, it's working. Thanks for your help, one more thing, the condition is always going to be true. So why won't it work without the IF Statement?
    public class Donations
    {
       public static void main(String[] args)
       {
          Scanner keyboard= new Scanner (System.in);
          String donorName;
          double newDonation, donationQuantity=0, maxDonation=0, highestDonation=0;
          String highestDonor="";
     
          do
          {
             System.out.println("\n");
             System.out.println("Please enter donation amount");        //PRINT
             newDonation=keyboard.nextInt();
     
             System.out.println("Please enter your name");              //PRINT
             donorName=keyboard.next();
     
     
             if(newDonation>highestDonation)
             {
                highestDonation=newDonation;
                highestDonor=donorName;
             }
     
     
             donationQuantity=donationQuantity+1;
     
             maxDonation +=newDonation;
     
     
          }
          while (maxDonation<500);
     
          System.out.println("It took " + donationQuantity + " donations to reach the 500");
     
          System.out.println("Highest donation amount was " + highestDonation + " by " + highestDonor);
     
       }//main
    }//class

  4. #29
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,138
    Thanks
    48
    Thanked 2,156 Times in 2,129 Posts

    Default Re: Do While Loop

    the condition is always going to be true. So why won't it work without the IF Statement?
    Can you explain what "the condition" is you are talking about? And why it is always going to be true.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #30
    Member
    Join Date
    Feb 2013
    Location
    Canada
    Posts
    54
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Default Re: Do While Loop

    If newDonation <= 0, then highestDonation = newDonation. Since you're comparing whether newDonation is greater but not equal to highestDonation, the condition evaluates to false.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. For loop, the first command in the loop does not get executed the 2nd time..
    By lina_inverse in forum Loops & Control Statements
    Replies: 1
    Last Post: October 16th, 2012, 09:00 PM
  2. [SOLVED] Please help with my while loop that turned into infinite loop!
    By Hazmat210 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: March 10th, 2012, 10:22 PM
  3. Converting a while loop to a for loop and a for loop to a while loop.
    By awesom in forum Loops & Control Statements
    Replies: 3
    Last Post: February 26th, 2012, 07:57 PM
  4. For loop; Problems with my for loop
    By mingleth in forum Loops & Control Statements
    Replies: 5
    Last Post: November 16th, 2011, 06:24 PM
  5. [SOLVED] My while loop has run into an infinite loop...?
    By kari4848 in forum Loops & Control Statements
    Replies: 3
    Last Post: March 1st, 2011, 11:05 AM