主要类
import javax.swing.JOptionPane;
public class Shapes {
static String shape;
static String circleInput;
static String c;
static String r;
static String t;
static double radius;
public static void main(String[] args) {
// Creation of all three shape objects
Circle circleSolution = new Circle();
Rectangle solution2 = new Rectangle(shape, r);
Triangle solution3 = new Triangle(shape, t);
String choice = "y";
// Creation of a while loop which continues when 'choice' is 'y'
while (choice.equalsIgnoreCase("y")) {
// Prompts user for a shape or the interger 1 to end the program.
shape=
JOptionPane.showInputDialog ("Enter one of the following shapes:"
+ " circle, rectangle or triangle.");
// Converts the String 'shape' into an interger
if (shape.equalsIgnoreCase("circle")) {
shape = c;
circleInput = JOptionPane.showInputDialog ("Enter the radius.");
radius = Math.abs(Integer.parseInt(circleInput));
JOptionPane.showMessageDialog (
null, "The area of the circle is " + circleSolution.getCircleArea(), "Results",
JOptionPane.PLAIN_MESSAGE);
JOptionPane.showMessageDialog (
null, "The perimeter of the circle is " + circleSolution.getCirclePerimeter(), "Results",
JOptionPane.PLAIN_MESSAGE);
}
else if (shape.equalsIgnoreCase("rectangle")) {
shape = r;
// Prompts user for pertinent data
}
else if(shape.equalsIgnoreCase("triangle")) {
shape = t;
}
}
圈子对象类
public class Circle {
// Circle object created
Shapes circle = new Shapes();
// Defines variables used in class
private static String shape;
private static double radius;
private static String circleInput;
private static String c;
// Constructor for circles, with arguments
Circle() {
shape = Shapes.shape;
radius = Shapes.radius;
circleInput = Shapes.circleInput;
c = Shapes.c;
}
// Instance method used to return the shape
public String getShape() {
return shape;
}
// Instance method used to return the value of the circle's radius
public double getRadius() {
return radius;
}
// Instance method used return the string, circleInput
public String getCircleInput() {
return circleInput;
}
// Instance method used to return the specific shape
public String getC() {
return c;
}
double getCircleArea() {
double circleArea = Math.PI * radius * radius;
return circleArea;
}
double getCirclePerimeter() {
double circlePerimeter = 2 * Math.PI * radius;
return circlePerimeter;
}
}
对于圆的面积和周长,我得到0.0的结果。显然,这是不可取的。
如果您有任何疑问,请务必询问。
非常感谢任何和所有帮助/建议,提前谢谢!
答案 0 :(得分:0)
您的问题是您没有在Circle
对象实例中指定半径值。您获得输入,解析它,然后调用getCircleArea()
。
看来你正试图让Circle的构造函数使用主类中的静态半径变量作为半径。
我不会讨论为什么这是坏事(完全违背OOP范例),但这不会起作用,因为你已经创建了对象后分配了静态变量。
if (shape.equalsIgnoreCase("circle")) {
shape = c;
circleInput = JOptionPane.showInputDialog ("Enter the radius.");
radius = Math.abs(Integer.parseInt(circleInput));
circleInput.setRadius(radius); //DO THIS
JOptionPane.showMessageDialog (
null, "The area of the circle is " + circleSolution.getCircleArea(), "Results",
JOptionPane.PLAIN_MESSAGE);
答案 1 :(得分:0)
类的静态成员是默认初始化的
Sub InsertRow()
Dim time As Integer
Dim y As Integer
Dim colA As String
Dim colB As String
Dim colC As String
time = "FindTime"
colA = "Name1"
colB = "Value1"
colC = "Time1"
y = Application.Match(time, Columns(3), 0)
Rows(y).Insert Shift:=xlDown
Range(Cells(y - 1, "A"), Cells(y - 1, "C")).Borders(xlEdgeBottom).LineStyle = xlDouble
Cells(y, 1) = colA
Cells(y, 2) = colB
Cells(y, 3) = colC
End Sub
在这种情况下,static double radius;
被初始化为零。这就是用于计算面积和周长的内容。
额外注意事项:实例化包含应用程序主方法的类是非常不寻常的。
答案 2 :(得分:0)
嗯,您的代码有几个问题。可能是因为你误解了static
和一些面向对象概念的使用。
参数化您的构造函数:
如果你没有使用setter来设置对象的值,你至少需要一个参数化的构造函数来构造对象:
// Constructor for circles, with arguments
Circle(double radius)
{
radius = this.radius;
//Your other variables such as c & circleInput variable is not supposed to be here
}
删除所有不必要的静态修饰符:
class Circle{
private double radius;
}
public class Shapes {
private double radius;
}
将您的课程置于适当的等级制度下:
class Circle extends Shapes{
//radius inherited from Shapes now
}
public class Shapes {
private double radius;
}
circleInput
)与其他实例变量(如radius)混合。你也在你的形状类中写了你的主要方法,所有的变量混合在一起。您的学校可能希望您将它们分开。将变量写入应声明的位置:
class ShapesRunner(){ //Have a designated class for testing
public static void main(String[] args){
String circleInput, c, r, t;
//Your testing for the created shapes.
}
}
Java,对象编程:获取返回0值的方法
确保使用setter(如果有的话)或通过其构造函数设置圆的半径。
示例:强>
//receive radius from input
circle c = new Circle(radius);
//print c's area and parameter