The program I am trying to make should have a menu to select from that looks like this:
So, as you can see, the program is supposed to do math-calculations for a shape's surface-area. The
problem is that I can't get it to stop repeating the same question after it is finished. The code for the
program is below under the filenames AreaGiver.java (the main-program) and Geometry.java (the class
objects are made from for the main program). The java-comments will explain what I thought I was doing right, as well as the errors I realized in bold, parenthesized, capital-letters.
// This import-statement will be for Scanner-class input and output. import java.util.Scanner; // This import-statement will be for DecimalFormat-class numerical-setups. import java.text.DecimalFormat; public class AreaGiver { public static void main(String[] args) { // Here is a variable-setup for the these matched-up reasons: int areaChoice; // to hold the user's calculation-selection, double radius, // the radius of a circle, length, // the length of a rectangle, width, // the width of the rectangle, base, // the base of a triangle, height, // the height of the triangle, area; // the area of each shape, // to create a Scanner-class object named keys, Scanner keys = new Scanner(System.in); // and a DecimalFormat-class setup for the math-work results. DecimalFormat numSetup = new DecimalFormat("#,##0.00"); // Here, a menu is displayed by these statements, so the // numerical-choice can be made by the user. System.out.println("Geometry Calculator"); System.out.println("1. Calculate the Area of a Circle"); System.out.println("2. Calculate the Area of a Rectangle"); System.out.println("3. Calculate the Area of a Triangle"); System.out.println("4. Quit"); System.out.println(); System.out.println(); System.out.println("Enter your choice (1-4):"); areaChoice = keys.nextInt(); // This loop should execute each calculation-process based on the // selection while the areaChoice-variable is outside the range of 1-4. // ([B]THE IF-STATEMENTS WILL REPEAT IF YOU PRESS ENTER.[/B]) do { // This if statement will make the selection. if (areaChoice == 1) { // Get the circle's radius. System.out.println("What is the radius of your circle?"); radius = keys.nextDouble(); // Store the area in the area-variable for easier-reading. area = Geometry.getCircularArea(radius); // Display the circle's area. System.out.println("Your circle's area is: " + numSetup.format(area)); } else if (areaChoice == 2) { // Get the rectangle's length. System.out.println("What is the length of your rectangle?"); length = keys.nextDouble(); // Get the rectangle's width. System.out.println("What is the width of your rectangle?"); width = keys.nextDouble(); // Store the area in the area-variable for easier-reading. area = Geometry.getRectangularArea(length, width); // Show the area of the rectangle. System.out.println("Your rectangle's area is: " + numSetup.format(area)); } else if (areaChoice == 3) { // Get the triangle's base. System.out.println("What is the base of your triangle?"); base = keys.nextDouble(); // Get the triangle's height. System.out.println("What is the height of your triangle?"); height = keys.nextDouble(); // Put the area in the area-variable for easier-reading. area = Geometry.getTriangularArea(base, height); // Show the triangle's area. System.out.println("Your triangle's area is: " + numSetup.format(area)); } else if (areaChoice == 4) { // Show that the program ends here. System.out.println("Program ending"); } else { // Show an error-message to get a number within the range of 1-4. System.out.println("Enter a number within the range of" + " 1-4."); areaChoice = keys.nextInt(); } } while (areaChoice <= 0 && areaChoice >= 5); // Here, make the choice the user made. } }
// Use this Scanner-class import-statement for output. import java.util.Scanner; public class Geometry { // The getCircularArea-method should calculate the area of a circle // and give an error-message to get the right number-type. public static double getCircularArea(double radius) { // Create a Scanner-class object for output. Scanner keys = new Scanner(System.in); // Give an error-message for a value of 0 or below. // ([B]THIS LOOP WILL KEEP REPEATING UNLESS YOU PRESS CTRL + C.[/B]) while (radius <= 0) { System.out.println("Do not enter a value of 0 or below."); } // Return the circle's area. return Math.PI * Math.pow(radius, 2.0); } // The getRectangularArea-method should calculate the area of a rectangle // and give an error-message to get the right number-type. public static double getRectangularArea(double length, double width) { // Create a Scanner-class object for output. Scanner keys = new Scanner(System.in); // Give an error-message for a value of 0 or below. // ([B]THIS LOOP WILL KEEP REPEATING UNLESS YOU PRESS CTRL + C.[/B]) while (length <= 0 && width <= 0) { System.out.println("Do not enter values of zero or below."); } // Return the rectangular-area. return length * width; } // The getTriangularArea-method should calculate the area of a triangle // and give an error-message to get the right number-type. public static double getTriangularArea(double base, double height) { // Create a Scanner-class object for output. Scanner keys = new Scanner(System.in); // Give an error-message for a value of 0 or below. // ([B]THIS LOOP WILL KEEP REPEATING UNLESS YOU PRESS CTRL + C.[/B]) while (base <= 0 && height <= 0) { System.out.println("Do not enter values of zero or below."); } // Return the triangular-area. return base * height * 0.5; } }
If there is anything erroneous about the posting's presentation, let me know. (I'm new to this.)