#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    7
    Rep Power
    0

    Prompt to continue program


    Hello:
    I have a question. I have the basic structure of my code done. It is taking a radius input from the console and outputting the area and perimeter. Once this completes the user should be prompted if they want to continue. If they say yes it will ask them for the radius and output. If they say no, the program will end. I am unsure how/where to insert a while loop to handle this. Any help would be appreciated. I tried a few different ways and could not get it so I wanted to reach out for guidance on this. Thank you. Here is my main class:

    Code:
    public class Circle
    {
       //declare instance variables
       
       private double radius;
       final private double PI = 3.14159;
       
       //constructor
       public Circle (double radius )
       {
        this.radius = radius;
        }
          
       /**constructor:
        * Sets initial values of instance variable
       */ 
       public Circle ()
       {
         radius = 0.0;
        }
        
        //method sets Circle radius
        
        public void setRadius ( double radius )
        {
            this.radius = radius;
        }
        
       //method returns the radius of the circle
        
       public double getRadius()
       {
           return radius;
        }
        
        //method returns the area of the circle
        
        public double getArea()
        {
            return PI * radius * radius;
        }
        
        //method returns the diameter of the circle
        
        public double getPerimeter()
        {
            return 2 * PI * radius;       
        }
        
        
    }
    Here is my client class where I am trying to figure out where to input the loop.

    Code:
    //import scanner for use
    import java.util.Scanner;
    
    
    public class CircleTest
    {
    public static void main ( String [] args )
    {
    //declare variable to hold a radius value
    double radius;
    
    //initialize scanner for use
    Scanner scan = new Scanner ( System.in );
    
    //prompt user for radius value
    System.out.print ( "Please enter the circle's radius: ");
    
    //set variable value to user input
    radius = scan.nextDouble();
    
    //create and instance of the Circle class and pass data to constructor
    Circle circle1 = new Circle ( radius );
    
    //Output the circle data
    System.out.print ( "The area of the circle is " + circle1.getArea() + " and with a perimeter of " + circle1.getPerimeter());
    }
    
    
    
    
       
    }
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    Wrap the code you want repeated inside of the loop.

    begin loop
    ask for input
    read input
    compute results
    show results
    ask user if wants to continue
    exit loop if not
    end loop
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    7
    Rep Power
    0

    Do While


    So, would that be putting in a Do While loop in the beginning of the code such as:

    Code:
    //import scanner for use
    import java.util.Scanner;
    
    
    public class CircleTest
    {
    public static void main ( String [] args )
    {
    //declare variable to hold a radius value
    double radius;
    
    //declare variable to hold user input on continuing play
    String answer;
    
    //initialize scanner for use
    Scanner scan = new Scanner ( System.in );
    
    //create loop to see if user wants to continue operation
    do
    {
    
    //prompt user for radius value
    System.out.print ( "Please enter the circle's radius: ");
    
    //set variable value to user input
    radius = scan.nextDouble();
    
    //create and instance of the Circle class and pass data to constructor
    Circle circle1 = new Circle ( radius );
    
    //Output the circle data
    System.out.print ( "The area of the circle is " + circle1.getArea() + " and with a perimeter of " + circle1.getPerimeter());
    }
    while (!answer.equals("no") && !answer.equals("yes"));
    }
    
    
    
    
       
    }
    I am trying to get the syntax down for my code so that tonight after work and after the kids are in bed I can finish the code and compile it. I appreciate the guidance.

    Originally Posted by NormR
    Wrap the code you want repeated inside of the loop.

    begin loop
    ask for input
    read input
    compute results
    show results
    ask user if wants to continue
    exit loop if not
    end loop
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    Looks like you skipped this step:
    ask user if wants to continue
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    7
    Rep Power
    0

    update


    My bad.

    Code:
    //import scanner for use
    import java.util.Scanner;
    
    
    public class CircleTest
    {
    public static void main ( String [] args )
    {
    //declare variable to hold a radius value
    double radius;
    
    //declare variable to hold user input on continuing play
    String answer;
    
    //initialize scanner for use
    Scanner scan = new Scanner ( System.in );
    
    //create loop to see if user wants to continue operation
    do
    {
    
    //prompt user for radius value
    System.out.print ( "Please enter the circle's radius: ");
    
    //set variable value to user input
    radius = scan.nextDouble();
    
    //create and instance of the Circle class and pass data to constructor
    Circle circle1 = new Circle ( radius );
    
    //Output the circle data
    System.out.print ( "The area of the circle is " + circle1.getArea() + " and with a perimeter of " + circle1.getPerimeter());
    
    System.out.print ( "Would you like to run the program again, yes or no? ");
    
    answer = scan.next();
    }
    while (!answer.equals("no") && !answer.equals("yes"));
    }
    
    
    
    
       
    }

    Originally Posted by NormR
    Looks like you skipped this step:
    ask user if wants to continue

IMN logo majestic logo threadwatch logo seochat tools logo