My program repeats certain displays after ENTER is pressed, as well as with loops
The program I am trying to make should have a menu to select from that looks like this:
Code Java:
Geometry Calculator
1. Calculate the Area of a Circle
2. Calculate the Area of a Rectangle
3. Calculate the Area of a Triangle
4. Quit
Enter your choice (1-4):
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.
Code java:
// 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.
}
}
Code java:
// 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.)
Re: My code-segment somehow executes again after I press RETURN, which isn't what I w
Please do not duplicate threads. I have merged your threads (somehow the merge went a bit too far and merged 3 posts into one, I edited your post above to avoid confusion - sorry about that).
Check the while condition
Code :
while (areaChoice <= 0 && areaChoice >= 5);
a value cannot be <= 0 and >= 5
Re: My code-segment somehow executes again after I press RETURN, which isn't what I w
SOG, here. Thank you for your help. Now I just need to understand why the do-while loop won't repeat after a correct error-message response.
Re: My code-segment somehow executes again after I press RETURN, which isn't what I w
Two things I noticed.
1.) You never actually told it
System.exit(0); for your ending code, so it won't actually ever end.
2.) If you put that menu to pop up inside the do while loop, then it might repeat if given a bad answer. In fact, it'll repeat if given a good answer too. As is, there's no way for the user to enter another value unless it's inside the loop.
3.) while (base <= 0 && height <= 0)
{
System.out.println("Do not enter values of zero or below.");
}
Assuming you don't want a base OR a height to be less than or equal to 0, you should change that && to ||
Otherwise it will only go through the loop if BOTH are less than or equal to 0.
Re: My code-segment somehow executes again after I press RETURN, which isn't what I w
Quote:
Originally Posted by
javapenguin
1.) You never actually told it
System.exit(0); for your ending code, so it won't actually ever end.
There is absolutely no reason the code should make a call to exit.
Re: My code-segment somehow executes again after I press RETURN, which isn't what I w
This thread is now solved. I was supposed to add an input-getting statement after my error-messages in Geometry.java and make a while-loop before the if-else-if statement in AreaGiver.java. All I needed were some of these responses.