代码正确,但我需要在原输出和排序后输出之间空一行
时间: 2024-03-15 08:44:53 浏览: 20
好的,可以在输出前加上一个换行符。修改后的代码如下:
```
import java.util.Arrays;
public class Student implements Comparable<Student>{
private String name;
private double height;
private double weight;
private double score;
public Student(String name, double height, double weight, double score) {
this.name = name;
this.height = height;
this.weight = weight;
this.score = score;
}
public void speak() {
System.out.println("I am " + name + ",my height " + height + ",my weight " + weight + ",my score " + score);
}
@Override
public int compareTo(Student o) {
double weightSum = height * 0.3 + weight * 0.3 + score * 0.4;
double oWeightSum = o.height * 0.3 + o.weight * 0.3 + o.score * 0.4;
if(weightSum > oWeightSum) {
return 1;
} else if(weightSum < oWeightSum) {
return -1;
} else {
return 0;
}
}
public static void main(String[] args) {
Student[] students = new Student[6];
students[0] = new Student("zhangsan", 170.0, 110.0, 95.0);
students[1] = new Student("lisi", 168.0, 120.0, 75.0);
students[2] = new Student("wangwu", 165.0, 115.0, 88.0);
students[3] = new Student("zhaoliu", 172.0, 121.0, 90.0);
students[4] = new Student("zhouqi", 160.0, 100.0, 85.0);
students[5] = new Student("zhengba", 166.0, 119.0, 70.0);
System.out.println("array sort before:\n");
for(Student student : students) {
student.speak();
}
Arrays.sort(students);
System.out.println("\narray sort after:\n");
for(Student student : students) {
System.out.println(student.toString());
}
}
@Override
public String toString() {
return "Student [name=" + name + ",height=" + height + ",weight=" + weight + ",score=" + score + "]";
}
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)