User inputs information to find the area of one of the three shapes. It works fine, but I need it to round to the second decimal. For example when you enter 2.5 for the radius of the circle, it outputs "Area of the circle is 19", rather than what I want it to, "Area of the circle is 19.63". Here is all of the code first:
package areas; import java.util.Scanner; public class triangle_rectangle_circle { public static void main(String[] args) { System.out.println("This program was written by Dan"); System.out.println("-----------------------------"); String varName; System.out.print("Enter your name: "); Scanner stringScanner = new Scanner(System.in); //string scanner varName = stringScanner.nextLine(); String transChar; System.out.print("Please select a shape: (R)ectangle, (T)riangle, or (C)ircle: "); transChar = stringScanner.nextLine(); //transChar = transition to char char caseSelector = transChar.charAt(0); System.out.println("-----------------------------"); Scanner numScanner = new Scanner(System.in); //number scanner switch (caseSelector){ //Case: Rectangle case 'r': case 'R': double rLength; double rWidth; System.out.print("Please enter width: "); rLength = numScanner.nextDouble(); System.out.println("Please enter height: "); rWidth = numScanner.nextDouble(); double rArea = (rLength * rWidth); System.out.print("Area of the rectangle is " + Math.round(rArea * 100 / 100)); System.out.println(); break; //Case: Triangle case 't': case 'T': double tBase; double tHeight; System.out.println("Please enter base:"); tBase = numScanner.nextDouble(); System.out.println("Please enter height: "); tHeight = numScanner.nextDouble(); double tArea = (.5 * tBase * tHeight); System.out.println("Area of the triangle is " + Math.round(tArea * 100) / 100); System.out.println(); break; //Case: Circle case 'c': case 'C': double cRadius; System.out.println("Please enter radius: "); cRadius = numScanner.nextDouble(); double cArea = (Math.pow (cRadius,2) * Math.PI); System.out.println("Area of the circle is " + Math.round(cArea * 100) / 100); System.out.println(); break; default: System.out.println("Please restart the program and enter a valid option"); break; } //switch (caseSelector) System.out.println("-----------------------------"); System.out.println("This program was run by: " + varName); stringScanner.close(); numScanner.close(); } //main method } //public class triangle_rectangle_circle
This is the part where I believe I'm going wrong:
case 'c': case 'C': double cRadius; System.out.println("Please enter radius: "); cRadius = numScanner.nextDouble(); double cArea = (Math.pow (cRadius,2) * Math.PI); System.out.println("Area of the circle is " + Math.round(cArea * 100) / 100); System.out.println(); break;calculates the areadouble cArea = (Math.pow (cRadius,2) * Math.PI);
is supposed to round the area to the second decimal by usingSystem.out.println("Area of the circle is " + Math.round(cArea * 100) / 100);Math.round(cArea * 100) / 100);
I'm not sure where I'm going wrong, I just need the 2 decimal places to be there when I enter a decimal, and it's not a integer. Ty!