编写程序,计算将水从初始温度加热到最终温度所需的能量。程序应该提示用户输入水的重量(千克),以及水的初始温度和最终温度。计算能量的公式是:
Q = M * (最终温度 - 初始温度) * 4184
*这里的M是以千克为单位的水的重量,温度以摄氏度为单位,而能量Q以焦耳为单位。
package 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 %.1f\n",
energy(AmountOfWater, initialTemperature, finalTemperature));
}
}
//能量 = 水重量(千克) * (最终温度 - 初始温度) * 4184
public static double energy(double AmountOfWater,
double initialTemperature, double finalTemperature) {
return AmountOfWater * (finalTemperature - initialTemperature) * 4184;
}
}