编写程序,计算将水从初始温度加热到最终温度所需的能量。程序应该提示用户输入水的重量(千克),以及水的初始温度和最终温度。计算能量的公式是:
Q = M * (最终温度 - 初始温度) * 4184
*这里的M是以千克为单位的水的重量,温度以摄氏度为单位,而能量Q以焦耳为单位。
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29package pack2; import java.util.Scanner; public class Energy { public static void main(String[] args) { try(Scanner input = new Scanner(System.in);) { System.out.print("Enter the amount of water in kilograms: "); double AmountOfWater = input.nextDouble(); System.out.print("Enter the initial temperature: "); double initialTemperature = input.nextDouble(); System.out.print("Enter the final temperature: "); double finalTemperature = input.nextDouble(); System.out.printf("The energy needed is %.1fn", energy(AmountOfWater, initialTemperature, finalTemperature)); } } //能量 = 水重量(千克) * (最终温度 - 初始温度) * 4184 public static double energy(double AmountOfWater, double initialTemperature, double finalTemperature) { return AmountOfWater * (finalTemperature - initialTemperature) * 4184; } }
最后
以上就是典雅方盒最近收集整理的关于Java、计算能量的全部内容,更多相关Java、计算能量内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复