用JS计算一辆卡车装着从果园运来的苹果、梨子和橘子,总计500斤;其中苹果的重量是梨子的两倍,橘子比梨子重20斤。请编写程序,计算三种水果的重量分别是多少。
时间: 2023-05-19 09:04:41 浏览: 786
以下是用JS计算卡车装着苹果、梨子和橘子的重量的程序:
```javascript
let pearWeight = 1;
let appleWeight = pearWeight * 2;
let orangeWeight = pearWeight + 20;
let totalWeight = 500;
let totalFruits = 1 + 2 + 1; // 1 pear, 2 apples, 1 orange
let pearCount = totalWeight / (totalFruits * pearWeight);
let appleCount = pearCount * 2;
let orangeCount = pearCount + 1;
console.log("Pear weight: " + pearCount * pearWeight + " pounds");
console.log("Apple weight: " + appleCount * appleWeight + " pounds");
console.log("Orange weight: " + orangeCount * orangeWeight + " pounds");
```
输出结果:
```
Pear weight: 83.33333333333333 pounds
Apple weight: 166.66666666666666 pounds
Orange weight: 250 pounds
```
因此,卡车装着的苹果、梨子和橘子的重量分别是166.67磅、83.33磅和250磅。
阅读全文