import java.util.*;
class CShape
{
public double pp()
{
}
public static void largest(CShape[] c)
{
double max=0.0;
for(int i=0;i<c.length;i++)
{
**if(c[i]>max)
{
c[i]=max;
} **
}
System.out.println("最大值"+max);
}
}
class CCircle extends CShape
{
double radius;
double pi=3.14;
public CCircle(double r)
{
radius=r;
}
public double pp()
{
return 2piradius;
}
}
class CTriangle extends CShape
{
double a,b,c;
public CTriangle(double x,double y,double z)
{
a=x;
b=y;
c=z;
}
public double pp()
{
return a+b+c;
}
}
class D10816317
{
public static void main(String[] args)
{
CShape[] cs = new CShape [4];
cs[0]=new CCircle(3);
cs[1]=new CCircle(7);
cs[2]=new CTriangle(3,4,5);
cs[3]=new CTriangle(4,8,9);
CShape.largest(cs);
}
}
在比大小那邊會出現兩個錯誤訊息 分別是
bad operand types for binary operator '>'
跟
incompatible type :double cannot be converted to CShape
想請問一下各位這種問題該怎麼解決
你的 c (cs) 定義如下
CShape[] cs = new CShape [4];
cs[0]=new CCircle(3);
cs[1]=new CCircle(7);
cs[2]=new CTriangle(3,4,5);
cs[3]=new CTriangle(4,8,9);
而你的 max = 0.0
錯誤訊息告訴你
你的 if c[i] > max 無法執行
因為 c[i] 是 CShape, max 是 double