私は、円、三角形、または長方形の周囲を計算できるプログラムを作成しようとしています。しかし、プログラムは正しく動作しません。メニューが表示されますが、何も入力できません。あなたのコードに若干の修正を加えた周辺電卓Java
public class GeometryDriver
{
public static void main(String[] args)
{
double radius;
double side1 = 0;
double side2 = 0;
double side3 = 0;
double length =0;
double width =0;
double perimeter = 0;
String userChoice;
Scanner keyboard = new Scanner(System.in);
int option;
System.out.println("Welcome to the Geometry Calculator!\n"
+ "In this program we will use a menu to decide what kind of shape we will create.\n"
+ "\n1.Create and Calculate Perimeter of a Circle"
+ "\n2. Create and Calculate Perimeter of a Rectangle"
+ "\n3. Create and Calculate Perimeter of a Triangle");
String input;
GeometricShape aShape = null;
option = keyboard.nextInt();
switch (option)
{
case 1:
input = JOptionPane. showInputDialog(null, "Enter the radius: ");
radius = Double.parseDouble(input);
aShape = new GeometricShape(radius);
perimeter = aShape.getPerimeter();
aShape = new GeometricShape(radius);
break;
case 2:
input = JOptionPane.showInputDialog(null, "Enter the length: ");
length = Double.parseDouble(input);
perimeter = aShape.getPerimeter();
input = JOptionPane.showInputDialog(null, "Enter the width: ");
width = Double.parseDouble(input);
aShape = new GeometricShape (length, width);
break;
case 3:
perimeter = aShape.getPerimeter();
input = JOptionPane.showInputDialog(null, "Enter side 1: ");
input = JOptionPane.showInputDialog(null, "Enter side 2: ");
input = JOptionPane.showInputDialog(null, "Enter side 3: ");
side1 = Double.parseDouble(input);
side2 = Double.parseDouble(input);
side3 = Double.parseDouble(input);
aShape = new GeometricShape (side1, side2, side3);
break;
default:
}
System.out.println(aShape + " has perimeter" + perimeter);
}
}
と...
public class GeometricShape
{
private double side1, side2, side3;
private double radius;
private double length, width;
private boolean triangle = false;
private boolean rectangle = false;
private boolean circle = false;`
public GeometricShape(double aSide1, double aSide2, double aSide3)
{
side1 = aSide1;
side2 = aSide2;
side3 = aSide3;
triangle = true;
}
public GeometricShape(double aLength, double aWidth)
{
length = aLength;
width = aWidth;
rectangle = true;
}
public GeometricShape(double aRadius)
{
radius = aRadius;
circle = true;
}
public double getRadius()
{
return radius;
}
public double getSide1()
{
return side1;
}
public double getSide2()
{
return side2;
}
public double getSide3()
{
return side3;
}
public double getLength()
{
return length;
}
public double getWidth()
{
return width;
}
public void setRadius(double aRadius)
{
radius = aRadius;
}
public void setSide1(double aSide1)
{
side1 = aSide1;
}
public void setSide2(double aSide2)
{
side2 = aSide2;
}
public void setSide3(double aSide3)
{
side3 = aSide3;
}
public void setLength(double aLength)
{
length = aLength;
}
public void setWidth(double aWidth)
{
width = aWidth;
}
public double getPerimeter()
{
double perimeter = 0;
if (triangle == true)
{
perimeter = side1 + side2 + side3;
}
else if (rectangle == true)
{
perimeter = (2* length) + (2*width);
}
else if (circle == true)
{
perimeter = 2* Math.PI * radius;
}
}
public String toString()
{
return "The perimeter is: " + this.getPerimeter();
}
}
あなたが見てみる必要があるスキャナやダイアログボックスを使用してスティックを簡単にするためにお勧めします私はちょうどダイアログボックスでは、実行されているウィンドウの背後に表示される問題...を実現 – pvg