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]);
}
}
}
public String toString() {
^
1 error
return "Customer [name="+name+",height="+height+",weight="+weight+"]";
}
}
这一段错误
给你改好了,采纳一下,亲
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;
}
}
你的compareTo方法的大括号多了一个吧
你这是写toString方法报错?