Reputation: 1
Decimal format is not working, but believed to be typed up correctly calculation for area isn't working keeps giving me 0.0 circumference works. I need help getting it to skip entering radius when they enter an invalid or option. Please help. Attached is the program:
import java.text.DecimalFormat;
import java.util.Scanner;``
public class Calculator {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
DecimalFormat formatter = new DecimalFormat("#00.0000");
String choice;
int option;
double area;
double circumference;
double radius;
area = 0;
circumference = 0;
option = keyboard.nextInt();
radius = keyboard.nextDouble();
System.out.println("CIRCLE CALCULATOR MENU");
System.out.println("1) Calculate the Area of a Circle");
if (option == 1) {
area = (radius*radius)*Math.PI;
}
System.out.println("2) Calculate the Circumference of a Circle");
if (option == 2) {
circumference = 2*radius*Math.PI;
}
System.out.println("3) Quit the Program");
System.out.println("");
System.out.println("Make a selection by choosing a number: ");
if (1 == option || 2 == option)
System.out.println("Please enter the radius of the circle: ");
if (1 == option) {
System.out.println("The area of the circle with radius "+radius+" is: " + circumference + "");
}else if (2 == option) {
System.out.println("The circumference of the circle with radius "+radius+" is: " + circumference + "");
} else if (option == 3) {
System.out.println("You have chosen to quit the program");
} else if (option > 4) {
System.out.println("You have made an invalid selection.");
}
}
}
Upvotes: 0
Views: 117
Reputation: 3785
You have rearrange your code. Its not in proper order:
Try this :
while(true) {
Scanner sc = new Scanner(System.in);
System.out.print("Please enter your option 1.Area 2.Circumference 3.Quit");
int choice = sc.nextInt();
double area = 0,circumference =0, radius=0;
switch (choice) {
case 1:
System.out.print("PLease enter the radius :");
radius = sc.nextInt();
area =(radius*radius)*Math.PI;
System.out.print("Area : " +area);
continue;
case 2:
System.out.print("PLease enter the radius :");
radius = sc.nextInt();
circumference = 2*radius*Math.PI;
System.out.print("circumference : " +circumference);
continue;
case 3:
break;
default:
System.out.print("Enter a valid option");
}
}
Upvotes: 0
Reputation: 3103
You are printing the value of circumference when you actually want to print the value of area:
System.out.println("The area of the circle with radius " + radius + " is: " + circumference + "");
Change circumference
to area
in the above statement will solve the problem.
Upvotes: 1