(请提供完整补充代码)java定义一个立方体类,定义10个立方体,立方体用数组存储,并分别按照立方体的体积降序排列,体积相同时按照3边和升序排列(长+宽+高),3边和相同时,再按照长降序排列。
我的代码如下,但是只会按体积降序排列,不能同时满足题目中其他条件。
public class Cube {
float chang;
float kuan;
float gao;
float tj;
public Cube() {
super();
// TODO Auto-generated constructor stub
}
public Cube(float chang, float kuan, float gao) {
super();
this.chang = chang;
this.kuan = kuan;
this.gao = gao;
this.tj = chang*kuan*gao;
}
public float getChang() {
return chang;
}
public void setChang(float chang) {
this.chang = chang;
}
public float getKuan() {
return kuan;
}
public void setKuan(float kuan) {
this.kuan = kuan;
}
public float getGao() {
return gao;
}
public void setGao(float gao) {
this.gao = gao;
}
public float getTj() {
return chang*kuan*gao;
}
@Override
public String toString() {
return "Cube [chang=" + chang + ", kuan=" + kuan + ", gao=" + gao + ", tj=" + tj + "]";
}
}
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
public class pro2 {
public static void main(String[] args) {
Cube[] cus1=new Cube[10];
float chang,kuan,gao;
for (int i = 0; i < cus1.length; i++) {
chang =(float)(Math.random()*5+5);
kuan=(float)(Math.random()*1+3);
gao=(float)(Math.random()*5+10);
cus1[i]=new Cube(chang,kuan,gao);
}
Arrays.sort(cus1,new Comparator<Cube>() {
@Override
public int compare(Cube o1, Cube o2) {
if(o1.getTj()>o2.getTj())
return -1;
else if(o1.getTj()==o2.getTj())
return 0;
else
return 1;
}
});
for (int i = 0; i < cus1.length; i++) {
System.out.println(cus1[i].toString());
}
}}