Java 实现温度报告查询的循环
这项任务是计算一个热物体被放入冰箱后的温度。假设冷冻柜温度恒定在-20度。物体进入冷冻柜后,其温度每秒下降(K*dt)度,其中K=0.001,dt是当前物体温度和冷冻柜温度之间的差值。我做的主要任务是计算物体在冷冻室中放置一定时间后的温度(给定初始温度)。第二项任务要求我实施:Java 实现温度报告查询的循环,java,for-loop,Java,For Loop,这项任务是计算一个热物体被放入冰箱后的温度。假设冷冻柜温度恒定在-20度。物体进入冷冻柜后,其温度每秒下降(K*dt)度,其中K=0.001,dt是当前物体温度和冷冻柜温度之间的差值。我做的主要任务是计算物体在冷冻室中放置一定时间后的温度(给定初始温度)。第二项任务要求我实施: public static void temperatureReport(double initialTemperature) 该方法应打印对象在冷冻柜中放置0、10、20、30、40、50和60分钟后的温度(具有给定
public static void temperatureReport(double initialTemperature)
该方法应打印对象在冷冻柜中放置0、10、20、30、40、50和60分钟后的温度(具有给定的初始温度)。
解决方案必须使用循环。在循环的每次迭代中,它必须打印经过的分钟数和对象的当前温度。
输出应为表格格式,列可以左对齐,也可以右对齐。温度值应在小数点后精确显示一位数字。以下是我的完整代码:
package Homework;
public class Cooling {
public static final double FREEZER_TEMPERATURE = -20;
public static final double K = 0.001;
public static void main(String[] args) {
temperatureTest(70, 0);
temperatureTest(70, 60);
temperatureReport(70);
timeToCoolTest(70, -10);
timeToCoolTest(70, -20);
}
public static double temperature(double initialTemperature, int seconds) {
double currentTemp = initialTemperature;
for (int time = 1; time <= seconds; time++) {
currentTemp -= K * (currentTemp - FREEZER_TEMPERATURE);
System.out.printf("After %d seconds, the temperature of the object is %f%n", time, currentTemp);
}
return currentTemp;
}
// TODO: Implement method as specified in assignment brief
public static void temperatureReport(double initialTemperature) {
double currentTemp = initialTemperature;
}
// TODO: Implement method as specified in assignment brief
public static int timeToCool(double initialTemperature, double targetTemperature) {
return 0; }
public static void timeToCoolTest(double initialTemperature, double targetTemperature) {
System.out.println("### Time To Cool");
System.out.println("Initial temperature = " + initialTemperature);
System.out.println("Target temperature = " + targetTemperature);
int timeTaken = timeToCool(initialTemperature, targetTemperature);
System.out.println("Time to cool = " + timeTaken + " seconds\n");
}
public static void temperatureTest(double initialTemperature, int seconds) {
System.out.println("### Temperature Test");
System.out.println("Initial temperature = " + initialTemperature);
System.out.println("Seconds = " + seconds);
double temp = temperature(initialTemperature, seconds);
System.out.println("Temperature = " + temp + "\n");
}
}
打包作业;
公共级冷却{
公共静态最终双冷柜温度=-20;
公共静态最终双K=0.001;
公共静态void main(字符串[]args){
温度测试(70,0);
温度测试(70,60);
温度报告(70);
冷却时间试验(70,-10);
冷却时间试验(70,-20);
}
公共静态双温(双初始温度,整数秒){
双电流温度=初始温度;
对于(int time=1;time我将添加我的代码作为答案,因为注释部分不适合这样做。它看起来像这样:
public static void temperatureReport(double initialTemperature) {
for(int i = 0; i <= 60; i+=10) {
double currentTemp = Cooling.temperature(initialTemperature, i);
System.out.printf("After %d minutes, the temperature of the object is %f%n", i, currentTemp);
// TODO: Make the output like it has to be.
}
}
公共静态无效温度报告(双初始温度){
对于(int i=0;i公共静态双温度报告(双初始温度){
双电流温度=初始温度;
对于(int time=0;time),您可以将循环计数器增加10,并使用循环计数器调用温度
函数。代码段:for(unsigned int i=0;i@puelo ohh非常感谢你,这在方法内部会是什么样子?谢谢你的朋友,再问一个问题,我应该将时间乘以60吗,因为它是分钟,我之前使用了几秒钟的代码。我发布并回答了你给我的修改过的代码。是的。如果你需要分钟,你显然可以ed将秒乘以60。因此,不管它们是以10分钟为间隔?谢谢:)。这只是10个单位,Java不关心这是10秒还是10分钟。您也可以这样做:,以(inti=0;我很清楚,如果我用3600,我不需要乘以60。非常感谢你的时间,伙计:)。
public static double temperatureReport(double initialTemperature) {
double currentTemp = initialTemperature;
for(int time = 0; time <= 60; time+=10) {
currentTemp -= (time*60) *(K * (currentTemp - FREEZER_TEMPERATURE));
System.out.printf("After %d minutes, the temperature of the object is %f%n", time, currentTemp);
}
return currentTemp;
}