Angel丶奏弦ʕ๑•ɷ•๑ʔ 2022-05-15 09:16 采纳率: 33.3%
浏览 863
已结题

JAVA编程,定义一个Customer类

JAVA定义一个Customer类,包含姓名(name)、身高(height)、体重(weight),分数(score)以及speak()方法,该方法的功能是,输出自己的相关信息。 Customer类实现Comparable接口,实现比较两个Customer对象的大小,比较规则是:身高占20%权重,体重占30%权重,分数占50%权重,以综合计算三项权重和之后的值作为判断对象大小的依据。
import java.util.Arrays;

class Customer implements Comparable {
private String name;
private float height, weight, score;

    public  Customer(String  name,  float  height,  float  weight,  float  score)  {
            super();
            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(Customer  o)  {
                            

float s1=this.height+this.weight;
float s2=o.height+o.weight;
if(s1>s2) {
return 1;
}
if(s1<s2) {
return -1;
}
return 0;
}

    }

    public  String  toString()  {
            

return "Customer [name="+name+",height="+height+",weight="+weight+"]";

    }

}

public class TestCompare {
public static void main(String[] args) {
int i;
Customer ps[] = new Customer[6];
ps[0] = new Customer("zhangsan", 170, 110, 95);
ps[1] = new Customer("lisi", 168, 120, 75);
ps[2] = new Customer("wangwu", 165, 115, 88);
ps[3] = new Customer("zhaoliu", 172, 121, 90);
ps[4] = new Customer("zhouqi", 160, 100, 85);
ps[5] = new Customer("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]);
}
}
}

TestCompare.java:65: error: class, interface, or enum expected
public String toString() {
 ^

1 error

怎么解决啊, public String toString() {

return "Customer [name="+name+",height="+height+",weight="+weight+"]";

    }

}
这一段错误

该如何正确得到结果
  • 写回答

3条回答 默认 最新

  • 吕布辕门 后端领域新星创作者 2022-05-15 11:49
    关注

    给你改好了,采纳一下,亲

    img

    
    import java.util.*;
    import java.io.*;
    
    public class Test {
    
        public static void main(String[] args) {
            int i;
            Customer ps[] = new Customer[6];
            ps[0] = new Customer("zhangsan", 170, 110, 95);
            ps[1] = new Customer("lisi", 168, 120, 75);
            ps[2] = new Customer("wangwu", 165, 115, 88);
            ps[3] = new Customer("zhaoliu", 172, 121, 90);
            ps[4] = new Customer("zhouqi", 160, 100, 85);
            ps[5] = new Customer("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]);
            }
        }
    
    
    
    
    }
    
    class Customer implements Comparable{
        private String name;
        private float height, weight, score;
        public  Customer(String  name,  float  height,  float  weight,  float  score)  {
            super();
            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(Customer  o)  {
            float s1=this.height+this.weight;
            float s2=o.height+o.weight;
            if(s1>s2) {
                return 1;
            }
            if(s1<s2) {
                return -1;
            }
            return 0;
        }
    
    
        public  String  toString()  {
            return "Customer [name="+name+",height="+height+",weight="+weight+"]";
        }
    
        @Override
        public int compareTo(Object o1) {
            float s1=this.height+this.weight;
            Customer o=(Customer)o1;
            float s2=o.height+o.weight;
            if(s1>s2) {
                return 1;
            }
            if(s1<s2) {
                return -1;
            }
            return 0;
        }
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 7月11日
  • 已采纳回答 7月3日
  • 修改了问题 5月15日
  • 创建了问题 5月15日

悬赏问题

  • ¥15 netty整合springboot之后自动重连失效
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击