为温度报告查询实施循环

时间:2016-04-14 19:08:20

标签: java for-loop

任务是计算温暖物体放入冰箱后的温度。假设冷冻室温度恒定在-20度。一旦物体处于冷冻室中,其温度每秒下降(K * dt)度,其中K = 0.001并且dt是当前物体温度和冷冻器温度之间的差。我完成了主要任务,即在冷冻器中放置一定秒数后计算物体的温度(给定的初始温度)。第二项任务要求我实施:

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");
        }

    }

有人可以告诉我如何实施温度报告方法吗?非常感谢你:)。

2 个答案:

答案 0 :(得分:0)

我会将我的代码添加为答案,因为评论部分并不适合。它看起来像这样:

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.
    }
}

现在显然没有像你需要的那样格式化输出。这是我留给你的东西,因为这似乎是某种作业/作业。

答案 1 :(得分:0)

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;
        }