Angel丶奏弦。 2022-05-15 09:16 采纳率: 42.9%
浏览 915
已结题

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 关于IMageEnView 图标定位问题
  • ¥20 求解答(matlab)
  • ¥30 ffmpeg库使用过程中遇到的问题
  • ¥15 pyqt5 中python如何通过Qtwebchannel主动发消息给web前端
  • ¥15 关于HTML中title获取xml内容的问题
  • ¥15 fanuc机器人PRIO083数字信号未复原错误,如何解决?
  • ¥20 如何为现有电路板增加远程控制功能
  • ¥15 C#点击按钮的时候的循环次数就是最后一次,如何是循环第几次的值?
  • ¥15 UE5打包失败,求解决
  • ¥15 请问STM32G431的CANOPEN协议函数怎么写