我目前正在使用 joptionpane 构建一个几何程序。解析而不是使用 import java.scanner.util 会方便吗?我正在使用开关盒来列出形状和功能
public class
{
//
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
//ask user name
String user = JOptionPane.showInputDialog("Enter name please: ");
//list the menu using a switch case
while(true){
//ask user for list of shape choice
String choice = JOptionPane.showInputDialog("Greetings" + user + "Please choose from shapes below\n"+
"1.Rectangle\n"+
"2.Square\n"+
"3.Paralleogram\n"+
"4.Trapezoid\n"+
"5. Triangle\n"+
"6.Circle\n"+
"7.Rectangular solid\n"+
"8.Cube\n"+
"9.Right Circular Cylinder\n"+
"10.Sphere\n"+
"11.Right Circular Cone\n"+
"12.Square Pyramid\n"+
"13.Right Circular Cone Frustum\n"+
"14.End the program\n");
//switch case that get the 13 shapes
switch(choice){
case "1":
double length = get_Length();
double width = get_Width();
double calcArea = calcAreaOfRectangle(length, width);
Rectangle(length,width,calcArea);
break;
case "2":
double side = get_Side();
double calcSq = calcSquare(side);
//display
Square(side, calcSq);
break;
case "3":
double pLength = get_Length();
double height = get_Height();
double calcPArea = calcParallelogram(pLength, height);
Parallelogram(pLength,height,calcPArea);
break;
case "4":
//
double tHeight = get_Height();
double base1 = get_Base();
double base2 = get_Base2();
double calcTArea = calcTrapezoid(tHeight, base1, base2);
Trapezoid(tHeight, base1, base2, calcTArea);
break;
//triangle
case "5":
double triangleH = get_Height();
double baseTriangle = get_Base();
double calcTriangle = calcTriangle(triangleH, baseTriangle);
Triangle(triangleH, baseTriangle, calcTriangle);
break;
case "6":
double radius = get_Radius();
double calcCircle= calcCircle(radius);
Circle( radius, calcCircle);
case "7":
double rLength = get_Length();
double rWidth = get_Width();
double rHeight = get_Height();
double calcRectSolid = calcRectSolid(rLength,rWidth, rHeight);
break;
case "8":
double sideCube = get_Side();
double calcCube = calcCube(sideCube);
Cube(sideCube,calcCube);
break;
case "9":
double cylinderRadius = get_Radius();
double cylinderHeight = get_Height();
double calcRightCylinder = calcRightCylinder(cylinderRadius,cylinderHeight);
break;
case "10":
double sphereRadius = get_Radius();
double calcSphere = calcSphere(sphereRadius);
Sphere(sphereRadius,calcSphere);
break;
case "11":
double coneRadius = get_Radius();
double coneHeight = get_Height();
double calcCone = calcCone(coneRadius, coneHeight);
CircularCone(coneRadius,coneHeight,calcCone);
break;
case "12":
double squareLength = get_Length();
double squareWidth = get_Width();
double squareHeight = get_Height();
double calcSquarePyramid = calcSquarePyramid(squareLength,squareWidth,squareHeight);
SquarePyramid(squareLength,squareWidth,squareHeight,calcSquarePyramid);
break;
case"14":
JOptionPane.showMessageDialog(null,"Gg");
System.exit(0);
default:
JOptionPane.showMessageDialog(null,"Wrong option please choose 1-4");
}
}
}
public static double get_Length(){
//user input the length
double length;
Scanner scnr = new Scanner(System.in);
length = Double.parseDouble(JOptionPane.showInputDialog("Enter length:"));
//length = scnr.nextDouble();
return length;//return the lenght also
}
public static double get_Width(){
double width;
Scanner scnr = new Scanner(System.in);
width = Double.parseDouble(JOptionPane.showInputDialog("Enter width:"));
//width = scnr.nextDouble();
return width;
}
public static double get_Height(){
double height;
Scanner scnr = new Scanner(System.in);
height = Double.parseDouble(JOptionPane.showInputDialog("Enter height:"));
//height = scnr.nextDouble();
return height;
}
public static double get_Side(){
double side;
Scanner scnr = new Scanner(System.in);
side = Double.parseDouble(JOptionPane.showInputDialog("Enter side:"));
//side= scnr.nextDouble();
return side;
}
public static double get_Base(){
double base;
Scanner scnr = new Scanner(System.in);
base = Double.parseDouble(JOptionPane.showInputDialog("Enter base:"));
//base = scnr.nextDouble();
return base;
}
public static double get_Base2(){
double base2;
Scanner scnr = new Scanner(System.in);
base2 = Double.parseDouble(JOptionPane.showInputDialog("Enter base2: "));
//base2 = scnr.nextDouble();
return base2;
}
public static double get_Radius(){
double radius;
Scanner scnr = new Scanner(System.in);
radius = Double.parseDouble(JOptionPane.showInputDialog("Enter radius:"));
//radius = scnr.nextDouble();
return radius;
}
public static double calcAreaOfRectangle(double length, double width){
double calcArea;
Scanner scnr = new Scanner(System.in);
calcArea = scnr.nextDouble();
//The length must be greater than the width else display area
if(length<width){
JOptionPane.showMessageDialog(null,"The length must be greater than the width");
}
else {
//display the area
calcArea = length * width;
}
return calcArea;
}
public static double calcSquare(double side){
double calcSq;
calcSq = Math.pow(side,2);
return calcSq;
}
public static double calcParallelogram(double pLength, double height){
double calcPArea ;
calcPArea = pLength * height;
return calcPArea;
}
public static double calcTrapezoid(double tHeight, double base1, double base2){
//height times base1 plus base2
double calcTArea;
calcTArea = (1.0/2.0)* tHeight +(base1+base2);
return calcTArea;
}
public static double calcTriangle(double triangleH, double baseTriangle){
double calcTriangle;
calcTriangle = (1.0/2.0)* baseTriangle * triangleH;
return calcTriangle;
}
public static double calcCircle(double radius){
double calcCircle;
calcCircle = Math.PI * Math.pow(radius,
2);
return calcCircle;
}
public static double calcRectSolid(double rLength,double rWidth,double rHeight ){
double calcRectSolid;
calcRectSolid = rLength * rWidth * rHeight;
return calcRectSolid;
}
public static double calcCube(double sideCube){
double calcCube;
calcCube = Math.pow(sideCube,
2);
return calcCube;
}
public static double calcRightCylinder(double cylinderRadius,double cylinderHeight){
double calcRightCylinder;
calcRightCylinder = Math.PI * Math.pow(cylinderRadius, 2) * cylinderHeight;
return calcRightCylinder;
}
public static double calcSphere(double sphereRadius){
double calcSphere;
calcSphere = (4.0/3.0) *Math.PI * Math.pow(sphereRadius,3);
return calcSphere;
}
public static double calcCone(double coneRadius, double coneHeight){
double calcCone;
calcCone = (1.0/3.0) *Math.PI *Math.pow(coneRadius,2)*coneHeight;
return calcCone;
}
public static double calcSquarePyramid(double squareLength,double squareWidth, double squareHeight){
double calcSquarePyramid;
calcSquarePyramid = (1.0/3.0)*squareLength*squareWidth*squareHeight;
return calcSquarePyramid;
}
//display areaofSquare
public static void Rectangle(double length, double width,double calcArea){
//display the area of square
JOptionPane.showMessageDialog(null,"The area of rectangle is " + calcArea);
}
public static void Square(double side, double calcSq){
JOptionPane.showMessageDialog(null, "The area of square is " + calcSq);
}
public static void Parallelogram(double pLength,double height,double calcPArea){
//display area
JOptionPane.showMessageDialog(null,"The area of paralleogram is " + calcPArea);
}
public static void Trapezoid(double tHeight, double base1, double base2, double calcT){
//display area of trapezoid
JOptionPane.showMessageDialog(null,"The area of trapezoid is " + calcT);
}
public static void Triangle(double triangleH,double baseTriangle, double calcTriangle ){
JOptionPane.showMessageDialog(null,"The area of triangle is " + calcTriangle);
}
public static void Circle(double radius,double calcCircle){
JOptionPane.showMessageDialog(null,"The area of circle is " + calcCircle);
}
public static void RectangularSolid(double rLength, double rWidth,double rHeight,double calcRectSolid){
JOptionPane.showMessageDialog(null,"The area of rectangular solid " + calcRectSolid);
}
public static void Cube(double sideCube, double calcCube){
JOptionPane.showMessageDialog(null,"Area of cube is " + calcCube);
}
public static void RightCylinder(double cylinderRadius,double cylinderHeight,double calcRightCylinder){
JOptionPane.showMessageDialog(null,"ARea of right cylinder is" + calcRightCylinder);
}
public static void Sphere(double sphereRadius, double calcSphere){
JOptionPane.showMessageDialog(null,"Area of sphere is " + calcSphere);
}
public static void CircularCone(double coneRadius, double coneHeight,double calcCone){
JOptionPane.showMessageDialog(null,"Area of circular cone is " + calcCone);
}
public static void SquarePyramid(double squareLength,double squareWidth, double squareHeight,double calcSquarePyramid){
JOptionPane.showMessageDialog(null, "Area of square pyramid is " + calcSquarePyramid);
}
}
我跑了几次,我会随机选择矩形,它会询问长度。我给他们一个数字,它将停止执行。在此之后我想只是解析它然后当我运行它时,我选择矩形,这次询问长度和宽度。你会推荐解析它吗?