我正在尝试打印最小的区域和周边,但输出的所有区域都是圆类的周长和区域。这是我的主要方法(for循环一直在底部):
public static void main (String [] args)//print Figure(Figure[])
{
System.out.println("TESTING FIGURES");
System.out.println("===============\n\n\n");
// form an array of figures
System.out.println("We form an array of 4 figures");
Figure[] set1 = new Figure[4];
set1[0] = new Circle(10);
set1[1] = new Triangle(10, 6, 8);
try
{
set1[2] = new Triangle(5, 12, 7);
}
catch (IllegalArgumentException e)
{
System.out.println("We try to form an illegal triangle");
}
set1[2] = new Parallelogram(10, 20, Math.PI / 3);
set1[3] = new Square(6);
System.out.println("The array is ");
printArray(set1);
// find the figures with the largest area, largest perimeter,
// smallest area, smallest perimeter
Figure smallArea = getSmallestArea(set1);
Figure bigArea = getLargestArea(set1);
Figure smallPerimeter = getSmallestPerimeter(set1);
Figure bigPerimeter = getLargestPerimeter(set1);
// print these figures
System.out.print("\nThe figure with a largest perimeter is ");
printFig(bigPerimeter);
System.out.print("\nThe figure with a smallest perimeter is ");
printFig(smallPerimeter);
System.out.print("\nThe figure with a largest area is ");
printFig(bigArea);
System.out.print("\nThe figure with a smallest area is ");
printFig(smallArea);
}
// print an array of figures
// if the array is null or empty print the message "The array is empty"
// otherwise print 2 lines
// that displays the shape, the fields, the perimeter and the area
// of each item in the array
public static void printArray(Figure[] figs)
{
if(figs == null || figs.length == 0)
{
System.out.println("The array is empty");
}
else
for(int i = 0; i < figs.length; i++)
{
printFig(figs[i]);
}
}
// print the shape, the fields, the perimeter and the area
// of fig
// if fig = null, write null
public static void printFig(Figure fig)
{
if(fig == null)
System.out.println("null");
else
{
if( fig instanceof Circle)
{
System.out.print("a circle of ");
System.out.println( "radius = " + ((Circle)fig).getRadius() );
}
else if (fig instanceof Triangle)
{
System.out.print("a triangle with ");
System.out.println("sides " + ((Triangle)fig).getSide1() + ", " + ((Triangle)fig).getSide2() + ", " + ((Triangle)fig).getSide3());
}
else if (fig instanceof Parallelogram)
{
System.out.print("a parallelogram with ");
System.out.println("sides " + ((Parallelogram)fig).getSide1() + " and " + ((Parallelogram)fig).getSide2() + " and angle of " +
((Parallelogram)fig).getAngle());
}
else if (fig instanceof Square)
{
System.out.print("a square with ");
System.out.println("side= " + ((Square)fig).getSide());
}
System.out.println( "The perimeter is " + fig.getPerimeter( ) + " and the area is " + fig.getArea());
}
}
// return a reference to a figure with the largest perimeter
// among all figures of arr
// if arr is null or empty return null
public static Figure getLargestPerimeter(Figure[] arr)
{
if(arr == null || arr.length == 0)
return null;
Figure bigPerimeter = arr[0];
for(int i = 0; i < arr.length; i++)
{
if(arr[i] != null && arr[i].getPerimeter() > bigPerimeter.getPerimeter())
bigPerimeter = arr[i];
}
return bigPerimeter;
}
// return a reference to a figure with the smallest perimeter
// among all figures of arr
// if arr is null or empty return null
public static Figure getSmallestPerimeter(Figure[] arr)
{
if(arr == null || arr.length == 0)
return null;
Figure smallPerimeter = arr[0];
for(int i = 0; i > arr.length; i++)
{
if(arr[i] != null && arr[i].getPerimeter() < smallPerimeter.getPerimeter())
smallPerimeter = arr[i];
}
return smallPerimeter;
}
// return a reference to a figure with the largest area
// among all figures of arr
// if arr is null or empty return null
public static Figure getLargestArea(Figure[] arr)
{
if(arr == null || arr.length == 0)
return null;
Figure bigArea = arr[0];
for(int i = 0; i < arr.length; i++)
{
if(arr[i] != null && arr[i].getArea() > bigArea.getArea())
bigArea = arr[i];
}
return bigArea;
}
// return a reference to a figure with the smallest area
// among all figures of arr
// if arr is null or empty return null
public static Figure getSmallestArea(Figure[] arr)
{
if(arr == null || arr.length == 0)
return null;
Figure smallArea = arr[0];
for(int i = 0; i > arr.length; i++)
{
if(arr[i] != null && arr[i].getArea() < smallArea.getArea())
smallArea = arr[i];
}
return smallArea;
}
此外,我的三角类不会打印边,我试图输入IllegalArgumentException,但该方仍然无法打印。这是代码:
public class Triangle implements Figure
{
// the fields
private double a, b, c; // the 3 fields
// the constructor
// form a triangle with sides s1,s2,s3
// if s1,s2,s3 do not form a triangle, throw an
// IllegalArgumentException
public Triangle(double s1, double s2, double s3) //throws IllegalArgumentException
{
s1= a;//hypotenuse
s2= b;//base
s3= c;//height
if(a + b < c || a + c < b || c + b < a)
{
throw new IllegalArgumentException
("We try to form an illegal triangle");
}
}
// methods that return the 3 sides
public double getSide1()
{
return a;
}
public double getSide2()
{
return b;
}
public double getSide3()
{
return c;
}
// @ return the perimeter
@Override
public double getPerimeter()
{
return a + b + c;
}
// @return the area
@Override
public double getArea()
{
return (0.5) * b * c;
}
}
答案 0 :(得分:6)
for
循环中的第二个表达式意味着“在条件为真的情况下继续执行循环” - 而不是“继续执行UNTIL它是真的”。这意味着如果您的循环如下所示:
for(int i = 0; i > arr.length; i++)
你永远不会在循环中执行任何操作,因为i
从0开始,0 > arr.length
是false
,因此循环立即停止。将>
更改为<
。