编写一个类:以变量heigh为身高变量;以变量weigh为体重变量。计算三个人的体重平均值
时间: 2023-05-12 11:04:27 浏览: 197
好的,以下是一个简单的类实现:
```python
class Person:
def __init__(self, height, weight):
self.height = height
self.weight = weight
p1 = Person(180, 70)
p2 = Person(170, 60)
p3 = Person(190, 80)
average_weight = (p1.weight + p2.weight + p3.weight) / 3
print("Average weight:", average_weight)
```
这个类定义了一个 `Person` 类,每个实例都有 `height` 和 `weight` 属性。我们创建了三个人的实例,并计算了他们的平均体重。
相关问题
java编写一个类:以变量heigh为身高变量;以变量weigh为体重变量。计算三个人的体重平均值
以下是Java代码:
```
public class Person {
double height;
double weight;
public Person(double height, double weight) {
this.height = height;
this.weight = weight;
}
public static void main(String[] args) {
Person person1 = new Person(1.8, 70);
Person person2 = new Person(1.75, 65);
Person person3 = new Person(1.85, 80);
double averageWeight = (person1.weight + person2.weight + person3.weight) / 3;
System.out.println("三个人的体重平均值为:" + averageWeight);
}
}
```
编写一个类:以变量heigh为身高变量;以变量weigh为体重变量。以自己的身高、体重数
值初始化这两个变量,并实现一个方法,计算出BMI指数。
以下是实现该类的代码:
```
class BMI:
def __init__(self, height, weight):
self.height = height
self.weight = weight
def calculate_bmi(self):
bmi = self.weight / (self.height ** 2)
return bmi
```
你可以使用以下代码来初始化该类并计算BMI指数:
```
person = BMI(1.75, 70)
bmi = person.calculate_bmi()
print("BMI指数为:", bmi)
```
输出结果为:
```
BMI指数为: 22.857142857142858
```
希望这个答案能够帮到你!
阅读全文