java编写一个类:以浮点型将变量heigh为身高变量。按大小顺序输出身高
时间: 2023-05-12 11:04:40 浏览: 128
以下是Java代码:
```
public class Person {
private float height;
public Person(float height) {
this.height = height;
}
public float getHeight() {
return height;
}
}
public class Main {
public static void main(String[] args) {
Person person1 = new Person(1.75f);
Person person2 = new Person(1.68f);
Person person3 = new Person(1.82f);
Person[] people = {person1, person2, person3};
Arrays.sort(people, Comparator.comparing(Person::getHeight));
for (Person person : people) {
System.out.println(person.getHeight());
}
}
}
```
输出结果为:
```
1.68
1.75
1.82
```
相关问题
java编写一个类:以浮点型将变量heigh为身高变量;以变量weigh为体重变量。按大小顺序输出身高
和体重的值。
以下是 Java 代码:
```java
public class Person {
private float height;
private float weight;
public Person(float height, float weight) {
this.height = height;
this.weight = weight;
}
public void printHeightAndWeight() {
if (height > weight) {
System.out.println("Height: " + height + ", Weight: " + weight);
} else {
System.out.println("Weight: " + weight + ", Height: " + height);
}
}
}
```
你可以使用这个类来创建一个 Person 对象,并调用 `printHeightAndWeight()` 方法来输出身高和体重的值,按照大小顺序排列。
java编写一个类:以变量heigh为身高变量,以变量weight为体重变量。以自己的身高、体重数据作为变量的值输出
以下是Java编写的类,用于输出身高和体重数据:
```
public class HeightWeight {
public static void main(String[] args) {
double height = 170.0; // 声明身高变量
double weight = 60.0; // 声明体重变量
// 输出身高和体重数据
System.out.println("身高为:" + height + "cm");
System.out.println("体重为:" + weight + "kg");
}
}
```
这个类中,我们声明了一个双精度浮点型的身高变量和体重变量,分别赋值为170.0和60.0,然后使用`System.out.println()`方法输出这两个变量的值。运行这个类,会输出以下结果:
```
身高为:170.0cm
体重为:60.0kg
```
阅读全文