2.10(科学:计算能量)编写程序,计算将水从初始温度加热到最终温度所需的能量。程序应该提示用户输入水的重量(kg),以及水的初始温度、最终温度
题目
题目描述
2.10(科学:计算能量)编写程序,计算将水从初始温度加热到最终温度所需的能量。程序应该提示用户输入水的重量(kg),以及水的初始温度、最终温度
计算能量的公式
Q = M * (最终温度 - 初始温度) * 4184 能量Q:焦耳 水重量M:kg
运行示例
Enter the amount of water in kilograms: 55.5 Enter the initial temperature: 3.5 Enter the final temperature: 10.5 The energy needed is 1625484.0
代码块
import java.util.Scanner;
public class Test2_10 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter the amount of water in kilograms: ");
double M = input.nextDouble();
System.out.println("Enter the initial temperature: ");
double tem0 = input.nextDouble();
System.out.println("Enter the final temperature: ");
double tem1 = input.nextDouble();
double Q = M * (tem1 - tem0) * 4184;
System.out.println("The energy needed is " + Q);
}
}
零基础入门
从控制台获取数据的语句顺序Pro
Scanner input = new Scanner(System.in);
System.out.println("Enter the amount of water in kilograms: ");
double M = input.nextDouble();
System.out.println("Enter the initial temperature: ");
double tem0 = input.nextDouble();
System.out.println("Enter the final temperature: ");
double tem1 = input.nextDouble();
如果出现以上情况,建议先Scanner语句 这样在本类/本方法,Scanner下面的代码都能从控制台获取数据 综合第1章题目,笔者建议: 先Scanner再print最后赋值
|