某大型家电企业拥有一批送货卡车,运送电视机、空调、洗衣机等家电。编程计算每个卡车所装载货物的总重量。
时间: 2023-06-05 19:47:03 浏览: 246
可以编写一个程序,输入每个卡车所装载的货物重量,然后将它们相加得到总重量。具体步骤如下:
1. 定义一个变量total_weight,用于存储总重量,初始值为。
2. 输入每个卡车所装载的货物重量,可以使用循环语句,每次循环输入一个货物重量,然后将其加到total_weight中。
3. 循环结束后,输出total_weight即可。
下面是一个示例代码:
total_weight =
num_trucks = int(input("请输入卡车数量:"))
for i in range(num_trucks):
weight = float(input("请输入第%d辆卡车所装载货物的重量:" % (i+1)))
total_weight += weight
print("所有卡车所装载货物的总重量为:%.2f" % total_weight)
相关问题
某大型家电企业拥有一批送货卡车,运送电视机、空调、洗衣机等家电。编程计算每个卡车所装载货物的总重量。 要求如下: 1.创建一个ComputeWeight接口,接口中有方法:double computeWeight(); 2.创建三个类Television、AirConditioner和WashMachine。这些类都实现了ComputeWeight接口,能够提供自重。其中TV的自重为16.6kg,AirConditioner的自重为40.0kg,WashMachine的自重为60.0kg。 3.创建一个Truck类,私有成员变量goods是一个ComputeWeight型的数组,包含了该货车上的所有家电,公有方法getTotalWeight()返回goods中所有货物的重量之和。
好的,根据您的要求,我已经明白了您的问题。下面是我的回答:
您需要创建一个ComputeWeight接口,其中包含一个计算重量的方法computeWeight()。然后创建三个类:Television、AirConditioner和WashMachine,它们都实现了ComputeWeight接口,并能够提供自重。TV的自重为16.6kg,AirConditioner的自重为40.0kg,WashMachine的自重为60.0kg。接着,您需要创建一个Truck类,其中包含一个私有成员变量goods,是一个ComputeWeight型的数组,包含了该货车上的所有家电。最后,您需要在Truck类中实现一个公有方法getTotalWeight(),返回goods中所有货物的重量之和。
具体实现代码如下:
```java
// ComputeWeight接口
public interface ComputeWeight {
double computeWeight();
}
// Television类
public class Television implements ComputeWeight {
private double weight = 16.6;
@Override
public double computeWeight() {
return weight;
}
}
// AirConditioner类
public class AirConditioner implements ComputeWeight {
private double weight = 40.0;
@Override
public double computeWeight() {
return weight;
}
}
// WashMachine类
public class WashMachine implements ComputeWeight {
private double weight = 60.0;
@Override
public double computeWeight() {
return weight;
}
}
// Truck类
public class Truck {
private ComputeWeight[] goods;
public Truck(ComputeWeight[] goods) {
this.goods = goods;
}
public double getTotalWeight() {
double totalWeight = 0;
for (ComputeWeight good : goods) {
totalWeight += good.computeWeight();
}
return totalWeight;
}
}
```
这样,您就可以通过实例化Truck类,并传入一个ComputeWeight型的数组,来计算该货车上所有家电的重量之和了。
JAVA接口实验:卡车要装载一批货物,货物由电视机、计算机和洗衣机组成,卡车需要计算出整批货物的重量。
可以通过定义一个接口来实现这个功能,代码如下:
```
interface Goods {
double getWeight();
}
class TV implements Goods {
private double weight;
public TV(double weight) {
this.weight = weight;
}
@Override
public double getWeight() {
return weight;
}
}
class Computer implements Goods {
private double weight;
public Computer(double weight) {
this.weight = weight;
}
@Override
public double getWeight() {
return weight;
}
}
class WashingMachine implements Goods {
private double weight;
public WashingMachine(double weight) {
this.weight = weight;
}
@Override
public double getWeight() {
return weight;
}
}
class Truck {
private List<Goods> goodsList = new ArrayList<>();
public void addGoods(Goods goods) {
goodsList.add(goods);
}
public double getTotalWeight() {
double totalWeight = 0;
for (Goods goods : goodsList) {
totalWeight += goods.getWeight();
}
return totalWeight;
}
}
public class Main {
public static void main(String[] args) {
TV tv1 = new TV(20);
TV tv2 = new TV(15);
Computer computer = new Computer(30);
WashingMachine washingMachine = new WashingMachine(50);
Truck truck = new Truck();
truck.addGoods(tv1);
truck.addGoods(tv2);
truck.addGoods(computer);
truck.addGoods(washingMachine);
System.out.println("Total weight of goods in the truck: " + truck.getTotalWeight() + " kg");
}
}
```
在这个例子中,我们定义了一个 `Goods` 接口和三个实现了 `Goods` 接口的类:`TV`、`Computer` 和 `WashingMachine`。这些类都实现了 `getWeight()` 方法来返回它们的重量。
然后我们定义了一个 `Truck` 类,它有一个 `goodsList` 成员变量来存储所有的货物。我们可以通过调用 `addGoods()` 方法来将货物添加到卡车中。`getTotalWeight()` 方法遍历所有货物并返回总重量。
最后,在 `Main` 类中,我们创建了一些货物并将它们添加到卡车中。然后我们调用 `getTotalWeight()` 方法来获取整批货物的总重量,并将其输出到控制台上。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)