求解如何将多个圆柱,根据他们的半径和高进行排序呢? 求指点……
你可以参考一下,希望采纳支持一下
1、Student类
public class Student implements Comparable{
//成员变量name
private String name;
//构造方法
public Student(String name){
this.name = name;
}
//get方法
public String getName(){
return this.name;
}
@Override
public int compareTo(Object O) {
Student s = (Student) O;
return this.name.compareTo(s.name);
}
}
StudentTest类
import java.util.Arrays;
public class StudentTest {
public static void main(String[] args){
Student[] StudentArray = {
new Student("tom"),
new Student("jerry"),
new Student("daniel"),
new Student("liang"),
new Student("liu"),
new Student("lin")
};
System.out.println("排序前:");
for(int i=0;i<StudentArray.length;i++){
if(i != StudentArray.length - 1)
System.out.print(StudentArray[i].getName() + ",");
else
System.out.print(StudentArray[i].getName());
}
Arrays.sort(StudentArray);
System.out.println("\n排序后:");
for(int i=0;i<StudentArray.length;i++){
if(i != StudentArray.length - 1)
System.out.print(StudentArray[i].getName() + ",");
else
System.out.print(StudentArray[i].getName());
}
}
}
程序执行结果如下:
2.文字闪烁
FlashingText类
import javax.swing.*;
public class FlashingText extends JFrame implements Runnable {
private JLabel jlblText;
public FlashingText() {
jlblText = new JLabel("Welcome",JLabel.CENTER);
add(jlblText);
new Thread(this).start();
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setBounds(400,300,300,300);
this.setVisible(true);
}
@Override
public void run() {
try{
while (true){
if (jlblText.getText()==null)
jlblText.setText("Welcome");
else
jlblText.setText(null);
Thread.sleep(200);
}
}catch (InterruptedException ex){
}
}
public static void main(String[] args) {
FlashingText ft = new FlashingText();
}
}
程序执行结果如下:
你自己定义的圆柱对象实现Comparable接口
如
public class 圆柱 implements Comparable<圆柱>{
@Override
public int compareTo(ComparableCircle o) {
return (int) (getArea()-o.getArea()); //面积相减之类
}
}
自己定义比较方法 比如 全面积 高 体积 之类的比较排序
你题目发出来吧。。。太不具体了