DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2
  1. #1
    Join Date
    Dec 2004
    Posts
    12

    do....while loop

    Hi,

    I have wrote a for loop that writes a program that asks the user to type in three numbers and then displays the total of those numbers

    public class Forloop3_3a
    {
    public static void main(String[] args)
    {
    int num1;
    int total = 0;
    for (int i=1; i<=3; i++ ) {
    System.out.print("Enter Numbers: ");
    num1 = EasyIn.getInt();
    total = total + num1;



    }
    System.out.println("Total is " + total);
    }
    }


    How can i do this using a do...while loop? so that you can type in numbers continually until you want to finish

    Thanks in advance

  2. #2
    Join Date
    Feb 2004
    Posts
    541
    Well you'll have a boolean variable (called something like notFinished), and you will use this to control the while loop. When the user wants to finish, set the boolean variable to false.
    Code:
    boolean notFinished = true;
    
    do
    {
        //get user input
    
        if(userWantsToEnd)
        {
            notFinished = false;
        }
    }
    while(notFinished);

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links