weixin_43408535
-cfyjbjonn
采纳率73.3%
2020-05-25 12:12 阅读 162
已采纳

关于java 简单问题11111111111111111111111111

5
import  java.util.Arrays;

class  Student  implements    
Comparable<Student>
{
        private  String  name;
        private  float  height,  weight,  score;

        public  Student(String  name,  float  height,  float  weight,  float  score)  {

this.name = name;
        this.height = height;
        this.weight = weight;
 this.score = score;

        }

        public  String  getName()  {
                return  name;
        }

        public  void  setName(String  name)  {
                this.name  =  name;
        }

        public  float  getHeight()  {
                return  height;
        }

        public  void  setHeight(float  height)  {
                this.height  =  height;
        }

        public  float  getWeight()  {
                return  weight;
        }

        public  void  setWeight(float  weight)  {
                this.weight  =  weight;
        }

        public  float  getScore()  {
                return  score;
        }

        public  void  setScore(float  score)  {
                this.score  =  score;
        }

        public  void  speak()  {

 System.out.println("I am " + name + ",my height " + height + ",my weight " + weight + ",my score " + score);


        }

        public  int  compareTo(Object  o)  {  //这一行不改变

int m = (int) (this.getWeight() * 0.3 + this.getHeight() * 0.3 + this.getScore()*0.4)
- (int) (o.getWeight() * 0.3 + o.getHeight() * 0.3 + o.getScore());
return m;

在上一行不改变的情况下,请问这里怎么修改才可以正确编译,谢谢


        }

        public  String  toString()  {

 return "Person [name=" + name + ",height=" + height + ",weight=" + weight + ",score=" + score + "]";

        }
}

public  class  TestCompare  {
        public  static  void  main(String[]  args)  {
                int  i;
                Student  ps[]  =  new  Student[6];
                ps[0]  =  new  Student("zhangsan",  170,  110,  95);
                ps[1]  =  new  Student("lisi",  168,  120,  75);
                ps[2]  =  new  Student("wangwu",  165,  115,  88);
                ps[3]  =  new  Student("zhaoliu",  172,  121,  90);
                ps[4]  =  new  Student("zhouqi",  160,  100,  85);
                ps[5]  =  new  Student("zhengba",  166,  119,  70);
                System.out.println("array  sort  before:");
                for  (i  =  0;  i  <  ps.length;  i++)  {
                        ps[i].speak();
                }

   Arrays.sort(ps); 
    //  call  sort  method
                System.out.println("\narray  sort  after:");
                for  (i  =  0;  i  <  ps.length;  i++)  {
                        System.out.println(ps[i]);
                }
        }
}

这道题只有一个错误,就是object,在不改变object的情况下,请问怎么改

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

3条回答 默认 最新

  • 已采纳
    caozhy 从今以后生命中的每一秒都属于我爱的人 2020-05-25 12:34
    要不然
    class  Student  implements  Comparable<Object>
    要不然
    public  int  compareTo(Student o)
    两个选一个
    
    点赞 评论 复制链接分享
  • csdnQiPa csdnQiPa 2020-05-25 14:57

    Comparable换成空的

    点赞 评论 复制链接分享
  • qybao qybao 2020-05-25 15:40

    Comparable改成Comparable或Comparable不带泛型
    public int compareTo(Object o) {方法的开头加上
    Student s = (Student)o;//强行转换,然后后面用到o的地方都改成s

    点赞 评论 复制链接分享

相关推荐