gpt4 book ai didi

java - 为什么这不会返回该区域?

转载 作者:行者123 更新时间:2023-12-01 10:04:03 24 4
gpt4 key购买 nike

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

最佳答案

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

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

关于java - 为什么这不会返回该区域?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36585814/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com