【问题标题】:Why will this not return the area?为什么这不会返回该区域?
【发布时间】:2016-04-12 23:20:45
【问题描述】:
class TestShapes  {

    public static void main(String[] args){
    Scanner input = new Scanner(System.in); // creates the scanner class
    System.out.print("Enter the numer of shapes: "); // asks user for input
    int N = input.nextInt(); // stores the user input as N
    Shape [] myShape = new Shape[N]; // will create as many shapes (N) in an array 
    for(int i=0;i<N;i++)
    {
        System.out.println("Enter the choice (Square, Rectangle, Circle):");
        int select = input.nextInt();
        if(select == 1)
        {
            //user wanted a Square
            System.out.print("Enter the color: ");
            String c = input.next();
            System.out.print("Enter the side length of the square: ");
            double s  = input.nextDouble();
            myShape[i] = new Square(c,s);

        }
        else if(select == 2)
        {
            //user wanted a Rectangle
            System.out.print("Enter the color: ");
            String c = input.next();
            System.out.print("Enter the length of the rectangle: ");
            double l = input.nextDouble();
            System.out.print("Enter the width of the rectangle: ");
            double w = input.nextDouble();
            myShape[i] = new Rectangle(c,l,w);

        }
        else if(select == 3)
        {
            //user wanted a Circle
            System.out.print("Enter the color: ");
            String c = input.next();
            System.out.print("Enter the radius of the circle: ");
            double r = input.nextDouble();
            myShape[i] = new Circle(c,r);

        }
}
    for(int i=0;i<N;i++) //this will print the details
    {
        System.out.println("\nShape "+ (i+1)+ ":");
        myShape[i].print();
    }
}

}

class Shape {

 String color;
 double area;

public Shape(){ // default constructor
    color = "red";
}

public Shape(String c){ // constructor
    color =c;
}

public String getColor(){ //accessors
    return color;
}

public void setColor(String c){//mutators
    color=c;
}
//print method
public void print()
{
    System.out.println("Color: "+ getColor());
}
 public double area(){
    return area;
}
}

class Square extends Shape{ // inherits from the shape class

double sideLength;

public Square(){//default constructor
    super();
    sideLength = 1;
}

public Square(String c, double s){ //constructor
    super(c);
    sideLength = s;
}

public double getSideLength(){//accessor
    return sideLength;
}

public void setSideLength(double s){//mutator
    sideLength = s;
}

public double area(){
    return sideLength * sideLength; //calculates the area of the square
}

public void print()
{
    super.print();
    System.out.println("Side length: " + getSideLength()
        + "\nArea: " + area);

}
}

class Rectangle extends Shape{// inherits from the shape class
    double length;
    double width;

public Rectangle(){//default constructor
    super();
    length = 1;
    width = 1;
}

public Rectangle(String c, double l, double w){ //constructor
    super(c);
    length = l;
    width = w;
}

public double getLength(){//accessor
    return length;
}

public double getWidth(){//accessor
    return width;
}

public void setLength(double l){//mutator
    length = l;
}
public void setSideLength(double w){//mutator
    width = w;
}

public double area(){
    return length * width; //calculates thea area of the rectangle
}

public void print()
{ // prints out the information
    super.print();
    System.out.println("Length: " + getLength() + "\nWidth:"+ getWidth() + "\nArea: "+ area);

}

}

class Circle extends Shape{// inherits from the shape class

    double radius;
public Circle(String c, double r){//default constructor
    super(c);
    radius = 1;
}

public Circle(double r){ //constructor
    super();
    radius = r;
}

public double getRadius(){//accessor
    return radius;
}

public void setRadius(double r){//mutator
    radius = r;
}

public void print()
{ // prints out the information
    super.print();
    System.out.println("Radius: " + getRadius() + "\nArea:"+ area);

}
public double area(){
return 3.14159 * (radius * radius); //calculates the area of the circle
}   
}

我已经尝试了所有让该区域返回的方法,但无论我尝试什么都不会。其他一切正常,它可以正确打印出所有内容,但不能正确打印该区域。有什么建议吗?

输入形状的数量:1 输入选项(方形、矩形、圆形): 1 输入颜色:红色 输入正方形的边长:2

形状 1: 红色 边长:2.0 面积:0.0

【问题讨论】:

    标签: java arrays


    【解决方案1】:

    你没有计算面积,你应该在打印语句中使用 area()

     System.out.println("Side length: " + getSideLength() + "\nArea: " + area());
    

    【讨论】:

    • 面积是在每个扩展形状的类(正方形、矩形、圆形)中计算的。他们在每个班级的底部。计算后返回。以上代码示例public double area(){ return sideLength * sideLength; //calculates the area of the square }
    • @154 家伙。正确,但是您在 print 语句中调用变量 area,而不是函数 area()
    【解决方案2】:

    您正在打印出未初始化的变量area,而不是调用函数area()

    System.out.println("Side length: " + getSideLength() + "\nArea: " + area());
    

    应该可以,但您应该避免使用同名的函数和变量。 getArea() 将是一个更好的函数名称。

    【讨论】:

    • 这行得通,谢谢。不好用它为将来的参考
    • 如果答案之一有助于解决您的问题,您应该接受。
    猜你喜欢
    • 2021-12-17
    • 2013-08-09
    • 2015-08-18
    • 2020-01-07
    • 2011-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多